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.