日期:2014-05-17  浏览次数:21200 次

C# C/S多窗体程序 showdialog后阻塞
登陆成功后,new了一个新窗体,并且需要在新窗体中与服务器沟通,现在可以把请求发给服务器,客户端这边执行到showdialog后就阻塞了,不能收到服务器发过来的命令了,客户端这边只有一个线程是用来与服务器沟通,客户端这边是不是需要一个新线程了执行showdialog方法?我的程序里有多个窗体,那是不是每执行一次showdialog方法,就需要创建一个新的线程呢?
contactWithServer = new ContactWithServer(this, user);
Thread clientcontactserverThread = new Thread(new ThreadStart(contactWithServer.ReceiveMsg));
            clientcontactserverThread.Start();
 


   
public void ReceiveMsg()
        {
            while (true)
            {
                lock (this)
                {
                    // 调用接收回调函数
                    IAsyncResult iar = udpReceiveClient.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState);
                    receiveDone.WaitOne();
                    Thread.Sleep(100);
                }
            }
        }
        private void ReceiveCallback(IAsyncResult iar)
        {
            UdpState udpReceiveState = iar.AsyncState as UdpState;
            if (iar.IsCompleted)
            {
                data = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
                if (data.Length < 20)                       //只有命令
                        commandStr = Encoding.ASCII.GetString(data);