简体   繁体   English

从线程触发时,C#ShowDialog()不会阻止主窗体

[英]C# ShowDialog() doesn't block main form when triggered from thread

I have a Windows Forms application with a main form and several background threads. 我有一个具有主窗体和几个后台线程的Windows Forms应用程序。 In order to show "nice" messages I implemented a form to be shown as a modal dialog using ShowDialog() . 为了显示“不错”的消息,我使用ShowDialog()实现了一种形式,可以显示为模式对话框。

All of the calls to ShowDialog() are implemented on form level of the main form (and not on thread level), but sometimes I trigger an event from a thread which will be handled on form level of the main form. ShowDialog()所有调用都是在主窗体的窗体级别(而不是线程级别)上实现的,但是有时我会触发线程中的事件,该事件将在主窗体的窗体级别上进行处理。 In those cases, the dialog is no longer modal ... the main form is not blocked. 在这种情况下,对话框不再是模态对话框...主窗体不会被阻止。 When the call to ShowDialog() is not triggered from a thread, it is modal as required. 当未从线程触发对ShowDialog()的调用时,根据需要它是模态的。

How can I make sure that every call to ShowDialog() will block the main form? 如何确保对ShowDialog()的每次调用都会阻止主窗体?

How can I make sure that every call to ShowDialog() will block the main form? 如何确保对ShowDialog()每次调用都会阻止主窗体?

Execute the calls to ShowDialog() on the main thread. 在主线程上执行对ShowDialog()的调用。 If you need to invoke them from a different thread, use Invoke() to run them on the main thread. 如果需要从其他线程调用它们,请使用Invoke()在主线程上运行它们。

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

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