tags:

views:

65

answers:

4

At the beginning of a section of C# code that could take several seconds to complete, I'd like to display a non modal form with a label that just says, "Please wait..."

WaitForm myWaitForm = null;

try
{
  // if conditions suggest process will take awhile
  myWaitForm = new WaitForm();
  myWaitForm.Show();

  // do stuff
}
finally
{
  if (myWaitForm != null)
  {
    myWaitForm.Hide();
    myWaitForm.Dispose();
    myWaitForm = null;
  }
}

The problem: the WaitForm doesn't completely display before the rest of the code ties up the thread. So I only see the frame of the form. In Delphi (my old stomping ground) I would call Application.ProcessMessages after the Show() Is there an equivalent in C#? Is there a canned "status" form that I can use in situations like this? Is there a better way to approach this?

Thanks in advance. David Jennings

+5  A: 

Yo need to run the do stuff part in a different thread.
and then all myWaitForm.Show()
Take a look at BackgroundWorker class here

Itay
Thanks very much. I have never looked at the BackgroundWorker. Looks like it simplifies multithreading a great deal. Might not be the best thing for this specific situation. But I'll definitely use it later in the same application.
David Jenings
A: 

The term you're looking for is "splash screen".

Here are a few related comments. http://stackoverflow.com/search?q=splash+screen+c%23

Brad Bruce
+1  A: 

You'd better move your "do stuff" code to other thread. and use Application.DoEvents() to proces the form messages

Arseny
+1  A: 

i agree on the "other thread" suggestion, but for simple and short purposes Application.DoEvents() will do.

vaitrafra