表单之间的跨线程调用. [英] Cross thread calls between forms.

查看:52
本文介绍了表单之间的跨线程调用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我制作了一个警报系统.它的作用是网络上的任何系统都在网络上广播UDP消息,而网络上的其他客户端会接收并显示该消息.
我想做的是,当主窗体收到警报消息时,它将显示带有该消息的其他窗体,并且如果广播该消息的计算机发送了取消警报消息,则应关闭第二个窗体(显示警报消息).所有的客户.

Hi, I have made an alarm system. What it does is that any system on the network broadcasts an UDP message on the network and other clients on the network receive that message and display it.
What I want to do is that, when the main form receives an alarm message it would show an other form with the message and if the computer which broadcasted the message sends a cancel alarm message the second form (showing alert message)should be closed on all the clients.

FrmAlertScreen frm = new FrmAlertScreen();

if (Convert.ToBoolean(alarm))//Set alarm to ring.
{
       status = "Ringing";
       RingAlarm = true;
       if (IsLocaly == false)//Alarm is not sent by local system then don't show alert screen.
       {
           frm.Show ();
       }

       IsLocaly = false;
}
else //Alram goes silent.
{
     
       status = "Silent";
       RingAlarm = false;
       frm.Hide() ;
       IsLocaly = false;
}


这段代码的问题是frm.show()使消息表单进入等待状态,可能是因为它在后台线程中运行.


Problem with this code is that frm.show() makes the message form to go into wait probably because it was running in background thread.

推荐答案

所有表单都应由应用程序的主线程.在另一个线程中进行通信.但是,一旦GUI需要显示某些内容,就让主线程执行.
这就是 Control.InvokeRequired [ ^ ]和
All forms should be handled by the application''s main thread. Do the communication in another thread. But as soon as the GUI needs to show something, let the main thread do it.
That''s what Control.InvokeRequired[^] and Control.Invoke[^] are for.


这篇关于表单之间的跨线程调用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆