I have a Windows Forms application with a BackgroundWorker. In a method on the main form, a MessageBox is shown and the user must click the OK button to continue. Meanwhile, while the messagebox is being displayed, the BackgroundWorker finishes executing and calls the RunWorkerCompleted event. In the method I have assigned to that event, which runs on the UI thread, the Close method is called on the form. Even though the method that shows the message box is still running, the UI thread is not blocking other threads from invoking methods on it. So the Close method gets called on the form. What I want is for the UI thread to block other threads’ invokes until the method with the message box has finished. Is there an easy way to do that?
Share
I think that you should be more disciplined about how your events are being handled. You don’t want to stop methods being invoked on the UI thread, because the method invocation mechanism is the same as the one that allows things like mouse and keyboard events to work. If you block method invocation you will also block that. Instead you need to delay the methods somehow. You may need to write your own synchronization code to do this.