tags:

views:

786

answers:

5

I basically need to show a wait window to the user. For this i have put two seperate window forms in the application. the first form is the main form with a button. The second one is a empty one with just a label text. On click of the button in Form1 i do the below

Form2 f = new Form2();
f.Show();
Thread.Sleep(2000);
f.Close();

My idea here is to show the wait window to the user for 2 second. But when i do this the Form 2 is not completely loaded because of which the label in it is blank. Please let me know your inputs on this.

+3  A: 

That's because you probably do some lengthy operation in the same thread (UI thread). You should execute your code in a new thread (see Thread class) or at least call Application.DoEvents periodically from inside your lengthy operation to update the UI.

Orlangur
I see no indication of a length operation beyond the call to Thread.Sleep. That delay is better handled by a Timer than a different thread - there's no need to either start a new thread or use Application.DoEvents.
Jon Skeet
As I understand Thread.Sleep is just a placeholder for some real code in there.
Orlangur
It's not clear - the question just talks about showing the user a "wait window". If the OP genuinely does want to do other work, then a thread (and BackgroundWorker) is appropriate - but I don't see any indication that that's the case here.
Jon Skeet
+1  A: 

You're basically blocking the UI thread.

I suggest that instead, you make your Form2 constructor (or possibly Load event handler) start a timer which will fire two seconds later. When the timer fires, close the form. During those two seconds, the UI thread will be free, so everything will display properly and the user will be able to move the window etc.

Jon Skeet
+1  A: 

When yo use Thread.Sleep you will disable the windows message loop and prevent the window from painting itself.

You could force a repaint:

f.Refresh();

Or better yet use a timer with a callback.

Timer t = new Timer();
t.Interval = 2000;
t.Tick += delegate { Close(); t.Stop();};
t.Start();

To prevent users from clicking in the original window you can open the new form as a dialog:

f.ShowDialog();
Cristian Libardo
A: 

You can (an always should for UI threads) use Thread.Current.Join(2000) instead of Thread.Sleep(2000).

Franci Penov
Calling Join on the current thread is a pretty odd way of getting round this issue, IMO. Much better to design it such that you don't need to just sleep - set a timer and continue your work when it fires. Re-entrancy is nasty :(
Jon Skeet
There's no re-entrancy when using Join() (unless you do Join() inside an STA COM object and another thread makes a call to it).On the flip side - something's changed in the Join() internals, as the form doesn't update when using it either.
Franci Penov
A: 

I think you should just use

f.ShowDialog(this);

Then control is returned when f is closed.

By using the sleep, you are blocking the UI thread from updating for 2 seconds. (The thread is asleep).

GvS