2013-04-15 58 views
0

我開發一個應用程序,而開發中我得到一個問題。所以我想解決它。我正在使用TCP服務器和客戶端概念。我寫的代碼很好。但阻止我的WPF窗口。我該如何解決它?誰能幫我。TCP連接阻止我的WPF窗口?如何獲取WPF窗口?

我的代碼

private void LoadingInboxMessage(){ 
try { 
    InboxTCPServer("127.0.0.1",1001); 
    string []header = new string[]{"Name","Mobile","Address"}; 
    SQL sql = new SQL(); 
    MySqlConnection con = sql.ConnectServer("127.0.0.1","root","''","smsdb"); 
    if(con!=null) { 
    MySqlDataReader dr = sql.SQLFetching(con,"SELECT * FROM receivedetails"); 
    CreateFlowDocument cfd = new CreateFlowDocument("inboxFlow"); 
    this.InboxFlowDocument.Document = cfd.CreateTable("inboxtable",header,dr); 
} 
    else { 
    MessageBox.Show("Not Connectioned"); 
    Application.Current.Shutdown(0); 
} 
} catch(Exception e) { 
    MessageBox.Show(e.Message); 
    } 
} 

public void InboxTCPServer(string ipv4, int port){ 
try { 
    IPAddress ip = IPAddress.Parse(ipv4); 
    TcpListener serv = new TcpListener(ip,port); 
    serv.Start(); 
    Socket s = serv.AcceptSocket(); 
    byte[] b = new byte[1000]; 
    int k = s.Receive(b); 
    MessageBox.Show(b.ToString()); 
} catch(Exception e) { 
    MessageBox.Show(e.Message); 
} 
} 

請 「救救我」

回答

1

你可能想看看在C#異步TCP/IP管理。我認爲你的問題是你掛起你的代碼,等待一個連接在同一個呈現UI的線程中,導致一個無響應的程序。 在這裏,你有幾個鏈接舉例:

+0

它不工作的哥哥! :(任何其他的解決方案 – Kernel

+0

你是怎樣在你的代碼中實現它 的其他簡單的解決辦法可能是從改變: 'InboxTCPServer(「127.0.0.1」,1001);' 到:?? '線程t = new Thread(InboxTCPServer(「127.0.0.1」,1001)); t.Start();' – misleadingTitle