简体   繁体   English

为什么消息框从不显示?

[英]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();
    }
  • I never see a messagebox display "hi". 我从没有看到消息框显示为“ hi”。
  • Second, do I still need to call EndInvoke(ar) when using Task.Factory from Async? 其次,从Async使用Task.Factory时是否仍需要调用EndInvoke(ar)

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.

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