Hello,
In my main window, when I have a lengthy process to run, I use a background worker so that the UI thread can display a "processing" window with a spinning wheel to let the user know something is happing so "please wait". When the background worker has completed it calls a method in the calling UI thread which in turn closes the modal "processing" window. The method that start the background worker and display the modal window looks like:
private void button1_Click(object sender, RoutedEventArgs e) {
worker = new InitBGWorker(this);
worker.Start();
DisplayProcessingWindow(true);
}
The method that the background worker calls when it has completed looks like:
public void BGWorkerCallback() {
DisplayProcessingWindow(false);
}
The DisplayProcessingWindow method looks like:
private void DisplayProcessingWindow(bool display) {
if (display) {
procWin = new InitControlLoadingWindow();
procWin.Owner = this;
procWin.WindowStartupLocation = WindowStartupLocation.CenterOwner;
procWin.ShowDialog();
}else {
procWin.Close();
}
}
Initially, all seems to work. While the background worker is doing its business, if you try to put focus on the main window, you can't do it. The problem I found, however, starts when the background worker is done. When the callback method is called by the background worker, the background worker thread is done, but the modal window is still displayed until the DisplayProcessingWindow method is called, as is intended. If the callback method needs to process some information and make some UI changes (thus it may take an extra few seconds before the modal window is closed), however, it appears that I can click on UI items on the main window even though the modal window is still open. When the UI thread is engaged again in the callback method, the spinning icon in the "processing" modal window stops spinning (I imagine because the UI thread now has other things it needs to do and can't refresh the animated GIF). I can understand that and that is fine. But during the time that the callback method is called until the time that the modal window is closed (at the end of the callback method), I am able to click on UI items in the main window (ie. the modal window is no longer acting in a modal fashion). You can't immediately tell that you can click on these UI components because it appears the events tied to them are not executed until the modal window is closed. For example, if I have a button that will append text to the value in a textbox, I can click it, say 3 times, in the callback method and then when the modal window is closed, the text added during each of the 3 button clicks will appear in the textbox. I wouldn't have expected that while the "modal" window was displayed. One way you can tell there is a difference is that if you try to click on the main window while the background worker is running, you can hear a beeping sound and sometimes a blinking of the window in the status tray, but when the background worker ends and the callback method is called, if you click on the main window you no longer hear the beeping or see the blinking even though the modal window is still open.
This behavior causes unanticpated problems. The goal of presenting a "processing" modal window is to prevent any user actions on the main window until the modal window is closed. Does anyone have any clues as to why this is happening? I really need the window to prevent any user actions on the main window until it is closed and not before.
Thanks - Peter