I have a case where, I need to print something in textbox1 then wait for a second, make an image visible and then again wait for a second then print something in textbox2 in one button click. When I wrote a sleep after the printing in textbox1 in the Click event of the button. I see that the printing on the UI is done all at a time, i.e. I expect it to be done sequentially one after another with a pause, but since its a single event handle it waits till end and show up on UI all at a time in the end.
Asked
Active
Viewed 716 times
2 Answers
0
I don't know if it work like for Windows phone 8 but you can "sleep" making the method async and calling
await Task.Delay(1000);
public async void myMethod(){
printString1();
await Task.Delay(1000);
showImage();
await Task.Delay(1000);
printString2();
}

Fabio Marcolini
- 2,315
- 2
- 24
- 30
-
well, I'd say that delay is asynchronous while sleep is synchronous – Fabio Marcolini Mar 28 '13 at 22:18
0
You can put your code in a thread, and use the dispatcher when you need to update the UI:
private void Process()
{
this.Dispatcher.BeginInvoke(() => this.TextBox1.Text = "Hello");
Thread.Sleep(1000);
this.Dispatcher.BeginInvoke(() => this.Picture1.Visibility = Visibility.Visible);
Thread.Sleep(1000);
this.Dispatcher.BeginInvoke(() => this.TextBox2.Text = "World");
}
Then, to start the thread (when the user clicks on the button):
var thread = new Thread(this.Process);
thread.Start();

Kevin Gosse
- 38,392
- 3
- 78
- 94