English 中文(简体)
服饰器在服用时的反应
原标题:A thread doesnt respond while app proccessing

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
                 ......
        }));

        }
    }

因此,对服务器的电文(每秒×秒)总是应当做出反应,询问他是否在服饰时活着。

问题回答

我认为,问题可能是:当你打电话Invoke时,全球倡议的更新是由主线(全球倡议一)进行的。 如果你主线忙于做其他事情(例如取消控制)不能立即处理这项任务,停止执行......

简言之,如果你向“国际倡议”穿透,就会出现这种情况。

Invoke(new MethodInvoker(delegate...

我可以建议你用服务器的电文进行盘问,并用时间对大约2或3秒的路口进行处理,它将更快地工作,使服务器的电文永远不会hang。

The main idea is to update UI not so often as it is done now.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签