How to print on UI and then make UI Thread wait for sometime in the same Click event in Windows phone app

StackOverflow https://stackoverflow.com/questions/15639809

Question

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.

Était-ce utile?

La solution

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();
}

Autres conseils

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();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top