P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

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();
    }

Upvotes: 1

Views: 98

Answers (1)

Frederik Gheysels
Frederik Gheysels

Reputation: 56934

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.

Upvotes: 5

Related Questions