In my winform application I have a form for displaying the ‘Please wait…’ message along with a progress bar or some text. For performance reasons the Form is a singleton which is opened using BringToFront(), Show() and Refresh() and it is closed with Hide().
I’m using try…finally pattern to display this form and hide it.
try
{
WaitForm.Display();
// Some code
}
finally
{
WaitForm.Hide();
}
In some rare cases, the users reported that the form didn’t close. Can you think of any reason why the form wouldn’t close? The user interface is responsive when this happens.
Typically, in windows application, for any UI refresh, its main message loop needs to be run. So in your case, if there is any tight loop/processing happening after this code block then form may not get hidden immediately.
Perhaps, you can yield the UI thread after the code-block so that UI can get refreshed. For example,