使用按钮 开个线程,此线程使用一个socket连接,等待socket server下发的信息,并更新界面listview
想做一个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)