To batch upload many files, there is one section of CSharp WPF code that I have which spawns threads to allow parallel uploading of files.
If an error occurs with the upload, a message would be popped up using MessageBox. This works fine.
But now recently we’ve decided to customize the look of the MessageBox. I find when our new custom WPF MessageBox window is opened with ShowDialog() that an error is thrown saying that it isn’t possible to launch a GUI. This only happens when code from the spawned thread attempts to open the custom Message window — I don’t see the problem when a custom MessageBox opens from the main thread.
My temporary fix is to catch any errors that occur when the custom dialog is launched and to alternatively call the standard MessageBox.Show() to display the message. That works.
I’m not sure why it’s OK to use the standard MessageBox.Show() but it isn’t for the custom window. Is there a way to get around this?
Generally (in any GUI-API) don’t call GUI functions from worker threads and don’t do heavy work on GUI threads.
The fact that the standard MessageBox works on a worker thread is sad.
Yes, you are completely right. When on your worker thread somethings happens that needs to be displayed to the user, send a notification (signal, event, whatever your Framework calls it) to the GUI thread and display the message there.