This project has moved. For the latest updates, please go here.

MessageBox : calling thread must be STA

Jul 26, 2011 at 9:14 AM

Hi,

I have a process that sends a request to the backend asynchronously. Now if the request to backend errored for some reason I want to show the error message dialog. But I keep getting the "calling thread must be STA" error. I understand it's not the in the current UI thread. But with Dispatcher.Current.BeginInvoke the message box does not show up at all. However if i replace the message box with the standard System.Windows.MessageBox I dont get this issue.

Jul 26, 2011 at 3:15 PM

You do not want to show messageboxes on a background thread.  Errors that occur should be handled in the RunWorkerCompleted handler.

private void RunWorkerCompletedHandler(object sender, RunWorkerCompletedEventArgs e)
{    
     if (e.Error == null)    
     {       
          DoSomethingWith(e.Result); // Access e.Result only if no error occurred.    
     }
     else
     {
          MessageBox.Show("Error");
     }
}