[英]Why does the messagebox never display?
Given 给定
static void Main()
{
Form f = new Form();
f.Show();
Action a = () => MessageBox.Show("hi");
Task.Factory.FromAsync(f.BeginInvoke(a), (ar) => a.EndInvoke(ar));
Console.Read();
}
When you call a MessageBox from a thread, other then the UI thread, it will never show up. 当您从线程(而不是UI线程)中调用MessageBox时,它将永远不会显示。
The correct way to handle this, is to raise an event from the method you're calling on another thread, and let the UI thread subscribe to it. 解决此问题的正确方法是,从另一个线程上调用的方法引发一个事件,然后让UI线程订阅该事件。 In the eventhandler, you can write code to display the MessageBox.
在事件处理程序中,您可以编写代码以显示MessageBox。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.