1

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

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
David Jenings
  • 593
  • 1
  • 4
  • 8

4 Answers4

5

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 Karo
  • 17,924
  • 4
  • 40
  • 58
  • 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 Jun 02 '10 at 13:44
1

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

Arseny
  • 7,251
  • 4
  • 37
  • 52
1

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

vaitrafra
  • 666
  • 3
  • 15
0

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

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

Community
  • 1
  • 1
Brad Bruce
  • 7,638
  • 3
  • 39
  • 60