2007-06-11 20:57:00
可以自己跟自己聊天 了,还没跟别人测试过,不知道真实网络下行不行
“数据到达”这个事件不知道有没有的,我现在是这样实现的: 连接后开新线程处理数据:
1msgthread = new Thread(new ThreadStart(MsgSolve));
2msgthread.Start();
这个“消息循环”里:
1private void MsgSolve()
2{
3 while (true)
4 {
5 if (ns.DataAvailable)
6 {
7 switch (ns.ReadByte())
8 {
9 case 2:
10 ns.Close();
11 tc.Close();
12 connected = false;
13 MessageBox.Show("对方断开了连接。", "信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
14 MyInvoke uui = new MyInvoke(UpdateUIAfterUnconnect);
15 this.BeginInvoke(uui);
16 return;
17 case 3:
18 byte[] length = new byte[4];
19 ns.Read(length,0,4);
20 byte[] bytes = new byte[BitConverter.ToInt32(length, 0)];
21 ns.Read(bytes, 0, bytes.Length);
22 string msg = "对方:/r/n" + Encoding.Unicode.GetString(bytes);
23 MyInvoke2 am = new MyInvoke2(AddMessage);
24 this.BeginInvoke(am, msg);
25 break;
26 }
27 }
28 }
29}
由于用了个无穷循环,导致占 CPU 100%,不知道真正的 Windows 消息循环是怎么实现空闲等待的
也不知道这里应该怎么搞,要是有这个事件就好了
首发:https://blog.csdn.net/cnStreamlet/article/details/1648188