Please Wait Dialog

G

Guest

I would like to display a please wait dialog while I perform long processes
in my WinForm. I have a MDI application, so I want the please wait dialog to
be the top form to prevent the users from performing any other functions
within the application.

If I use ShowDialog, and place my lengthy calls within the Dialog form, then
everything would work fine, but I want to have only one please wait dialog in
my application and call it from several forms.

Current I doing the following:

pleaseWait.Show()
pleaseWait.TopMost = true;

// do long stuff

pleaseWait.Close()

Unfortunately, pleaseWait.TopMost = true will keep the pleaseWait window on
top of all other applications. I only want pleaseWait to be the top most
window in my application.

Any ideas on accomplishing my goals?

Thanks for reading this long one.
Jake
 
T

Tom Krueger [MSFT]

You should be able to crate a callback scenario.

I haven't tried this, but it should work.

On the constructor of the PleaseWait form, require that a delegate gets
passed in. The delegate will be a pointer to a method you want to have
executed. Then in the form load event Invoke the delegate. Actually, in the
form load you will probably need to use BeginInvoke so the code runs
asychronously. This is so that the User Interface of the PleaseWait form
displays immediately and does not lock up while the processing occurs.

The problem now is how to get the form to close after the processing is
complete. To solve this, when you call BeginInvoke to execute the delegate,
be sure to specify a callback method. Then in the callback method you can
close the form as well as set the DialogResult if you want.

This should allow you to call the ShowDialog() and get the reuse you are
looking for.


PleaseWait frm = new PleaseWait(delegate);
DialogResult result = frm.ShowDialog();

if (result == DialogResult.Ok) {

}

if you need to pass different arguments to each method that gets processed
you will probably need to create a delegate that takes your a custom
EventArgs object.

I would look up Asynchronous Programming and Chris Sells Multithreading
articles for more information.

--
Tom Krueger

My Blog - http://weblogs.asp.net/tom_krueger
Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top