简体   繁体   中英

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".
  • Second, do I still need to call EndInvoke(ar) when using Task.Factory from Async?

When you call a MessageBox from a thread, other then the UI thread, it will never show up.

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. In the eventhandler, you can write code to display the MessageBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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