从线程触发时,C#ShowDialog()不会阻止主窗体 [英] C# ShowDialog() doesn't block main form when triggered from thread

查看:643
本文介绍了从线程触发时,C#ShowDialog()不会阻止主窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows Forms应用程序,其中包含一个主窗体和几个后台线程.为了显示不错"的消息,我使用ShowDialog()实施了一种形式,可以显示为模式对话框.

I have a Windows Forms application with a main form and several background threads. In order to show "nice" messages I implemented a form to be shown as a modal dialog using ShowDialog().

所有对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. 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()的调用.如果需要从其他线程调用它们,请使用Invoke()在主线程上运行它们.

Execute the calls to ShowDialog() on the main thread. If you need to invoke them from a different thread, use Invoke() to run them on the main thread.

这篇关于从线程触发时,C#ShowDialog()不会阻止主窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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