i have a c# Win Forms application i wrote and it has two threads -the main thread(the GUI) and a thread that is rensposible for responding to a server every x seconds (heartbeat). but when i run some code on the main thread(removing tabs,disposing things) the gui gets unresponsive and the thread isnt sending the heartbeats- what am i doing wrong? thanks :)
read:
thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
thrMessaging.Start();
而实际上,这是对从服务器上获取的所有信息负责的线索,因此:
private void ReceiveMessages()
{
// Receive the response from the server
srReceiver = new StreamReader(tcpServer.GetStream());
while (Connected)
{
// Show the messages in the log TextBox
//this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { srReceiver.ReadLine() });
try
{
String con = srReceiver.ReadLine();
string StringMessage = HttpUtility.UrlDecode(con, System.Text.Encoding.UTF8);
//MessageBox.Show("MESSAGE TRANSLATED:" + StringMessage);
processMessage(StringMessage);
}
catch (IOException e)
{
connectionTerminated();
}
}
}
由于需要, 有时,电传功能会预示一些国际倡议的任务:
private void processMessage(string p)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate
{
lastMessage = DateTime.Now;
//MessageBox.Show(p);
if (p == "AREYOUALIVE")
{
SendMessage("ye");
}
else if
......
}));
}
}
因此,对服务器的电文(每秒×秒)总是应当做出反应,询问他是否在服饰时活着。