想做一个tcp连接的客户端,和服务器建立连接之后,要等待服务器随时下发的消息,并更新form上的listview以显示下发的内容。
看了网上都是用线程和委托。但我写的都会在client.Receive(resp);这一句上阻塞,导致form也不动了。
请大侠给出个解决方法及涉及到的知识点(C#不懂初学)
下面是我的代码,但是界面总是卡死状态。请大侠指教啊。
private delegate void delegateWaitMessage(object tmp);
private void button1_Click(object sender, EventArgs e)
{
IPAddress ip = IPAddress.Parse(serverip);
IPEndPoint ipend = new IPEndPoint(ip, serverport);
try
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipend);
ParameterizedThreadStart pstart = new ParameterizedThreadStart(delegateDowaitForMessage);
Thread mythread = new Thread(pstart);
object suffixobj = myCarSuffix.ToString();
mythread.IsBackground = true;
mythread.Start(suffixobj);
}
}
private void delegateDowaitForMessage(object suffixobj)
{
string suffix = suffixobj.ToString();
RichTextBox richTxt;
if (!suffix.Equals("0"))
{
richTxt = (RichTextBox)(tabControl1.Controls.Find("richTextBox1" + suffix, true)[0]);
}
else
{
richTxt = richTextBox1;
}
while (true)
{
if (richTxt.InvokeRequired)
{
delegateWaitMessage objproxy = waitForMessage;
richTxt.Invoke(objproxy, new object[] { suffixobj });
}
}
}
private void waitForMessage(object suffixobj)
{
string suffix = suffixobj.ToString();
RichTextBox richTxt;
Socket client;
if (!suffix.Equals("0"))
{
richTxt = (RichTextBox)(tabControl1.Controls.Find("richTextBox1" + suffix, true)[0]);
client = socketmap["cars1" + suffix];
}
else
{
richTxt = richTextBox1;
client = socketmap["cars1"];
}
try
{
byte[] resp = new byte[256];
int resplen = 0;
resplen = client.Receive(resp);
string stringdata = Utils.byteToHexStr(resp, resplen);
string result = ParseResponse.ParseData(stringdata);
if (!result.Equals(""))
{
richTxt.AppendText(result + "\n");
richTxt.Update();
}
}
catch (Exception ex)
{
richTxt.AppendText(ex.Message + "\n");
}
}
------解决思路----------------------
幫頂
------解决思路----------------------
client.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0,
new AsyncCallback(Async_Send_Receive.Read_Callback), so);
------解决思路----------------------
socket连接和接收数据都用异步比较好,如果发送信息比较频繁,坚持设置成长连接。给你个网址吧,很全,可以参考下。
http://blog.csdn.net/ts_coo/article/category/1188088
------解决思路----------------------
楼主我看还是算了吧,初学先别接触这么深的东西。对你来说太难了。
------解决思路----------------------
异步编程模型(APM) 涉及到Socket通信、串口通信等等等
详细参考 http://www.cnblogs.com/xiaozhi_5638/p/3167794.html
------解决思路----------------------
一句话 解决 Task.Factory.StartNew(() => 你的函数名称());
你的函数名称 可以处理等待 更新Lable 等 不会阻塞主线程的.
4.0下可以这么用
楼上什么同步异步的方案 2.0下用用差不多,4.0在用这个太复杂,太落伍了
MS 就是知道自己的同步异步太麻烦了,才封装了一次,出现TPL方式
具体 可以百度 C# TPL
------解决思路----------------------
窗体:
//监听消息
StartUdpThread startUdpThread = new StartUdpThread(列表控件...等参数);//自定义的类
System.Threading.Thread startThread = new System.Threading.Thread(new System.Threading.ThreadStart(startUdpThread.StartUdp));//StartUdp未自定义监听方法
startThread.IsBackground = true;
startThread.Start();
StartUdpThread 类:
class StartUdpThread
{
private ListView list;
...
public StartUdpThread(ListView list,...)
{
this.list = list;
}
public void StartUdp()
{
System.Net.Sockets.UdpClient udpCLient = new System.Net.Sockets.UdpClient(2425);
System.Net.IPEndPoint ipEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any,0);
while (true)
{
//读取并处理消息
}
}
}