简体   繁体   English

在另一个线程中运行表单

[英]Run a form in another thread

i have five forms in my c# project. 我的C#项目中有五种形式。 one host an httplistener that i want to run continionsly. 一个主持人我想连续运行的httplistener。 when the listener gets a message, it passes it to a static class, which in turn calls the appropriate forms for another processing. 当侦听器收到消息时,它将消息传递给静态类,该类又调用适当的形式进行其他处理。

is it possible that the static class calls the new form in a new thread? 静态类是否有可能在新线程中调用新表单? if so please help me out 如果是这样,请帮帮我

The code in the forms should only deal with the visual aspects of the form. 表单中的代码应仅处理表单的视觉方面。 Anything else,especially http listeners, should be handled with separate classes running on background threads. 其他任何事情,尤其是http侦听器,都应使用在后台线程上运行的单独类来处理。

"Calling a form" doesn't mean anything, I guess you'd only want to Show() it. “调用表单”没有任何意义,我想您只希望Show()即可。 Creating a form on a worker thread is never a good idea. 在工作线程上创建表单绝不是一个好主意。 Even if you do get the thread state right (STA and message loop), you'll have hard-to-solve Z-order and modality problems. 即使您确实获得了正确的线程状态(STA和消息循环),也将有难以解决的Z顺序和模态问题。

Simply use Control.Invoke to run code on the UI thread. 只需使用Control.Invoke在UI线程上运行代码。 It should create the form and call its Show() method. 它应该创建表单并调用其Show()方法。

As Hans said, it may not be recommended, but if necessary, you can call System.Windows.Forms.Application.Run() from any thread. 正如汉斯所说,可能不建议这样做,但如有必要,可以从任何线程调用System.Windows.Forms.Application.Run() Just be careful and know what you are doing, if you do. 只要小心,就知道自己在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM