日期:2014-05-16  浏览次数:20849 次

c# 界面死掉了 求高手帮忙看看程序
做了一个button,点了button后,启动一段接收程序,button里的代码如下:
IPEndPoint  IP =new IPEndPoint(IPPAddress.Parse("172.17.1.150"),9192);
Socket UdpServer =new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocalType.Udp);
UdpServer.Bind(IP);
IPEndPoint  ipep=new IPEndPoint (IPAddress.Any,0);
EndPoint  Remote =(EndPoint )ipep;
int length=0;
new Thread(() =>
{
          while(true)
             {
                  byte[] data=new byte[10240];
                 try
                      {
                              length=UdpServer.ReceiveFrom(data,ref Remote);
                       }
                  catch(Exception ex)
                        {
                         }
                  textBox1.text += Encoding.GetEncoding("GBK").GetString(data,0,length)+Environment.NewLine;
             }
  }).Start();
这是一个通过socket UDP进行通信,但是点击button后,运行以上程序后,界面就不动了,但是textBox1一直在收发过来的内容!求大神帮忙看看,接收的同时为嘛不能进行其他操作了啊
------解决方案--------------------
跨线程访问UI用Invoke
            IPEndPoint IP = new IPEndPoint(IPPAddress.Parse("172.17.1.150"), 9192);
            Socket UdpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocalType.Udp);
            UdpServer.Bind(IP);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)ipep;
            int length = 0;
            new Thread(() =>
            {
                while (true)
                {
                    byte[] data = new byte[10240];
           &nb