Sleep () is executed in the wrong order.
My problem is pretty simple:
label1.Text = "Start";
Thread.Sleep(2000);
label1.Text = "Finish";
Why does Sleep () happen before the label changes to Start?
How can I change the shortcut, then sleep, and then change again?
+3
Taurophylax
source
to share
1 answer
The dream is happening in the right place, the problem is that you are missing the 4th step. What's really going on?
label1.Text = "Start";
Thread.Sleep(2000);
label1.Text = "Finish";
DrawUpdatedValuesOfLabel1OnTheUI();
The UI is not updated until execution returns to the message loop, you need to return control to the message loop while you wait two seconds to get the updated interface.
If you are using .NET 4.5, the easiest way to do this is to change Sleep
to using async / await
public async Task YourFunction()
{
label1.Text = "Start";
await Task.Delay(2000);
label1.Text = "Finish";
}
If you are not using .NET 4.5, the problem is much more difficult to solve.
+8
Scott chamberlain
source
to share