Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF not updating textbox while in progress

I have this code:

void wait(int ms)
{
    System.Threading.Thread.Sleep(ms);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    info.Text = "step 1";
    wait(1000);
    info.Text = "step 2";
    wait(1000);
    info.Text = "step 3";
    wait(1000);
    info.Text = "step 4";
    wait(1000);
}

And problem is that textbox.text is updated after whole void button1_Click finished. It is not updated ON AIR :(

Please, How to do that?

like image 906
DefinitionHigh Avatar asked Dec 22 '22 21:12

DefinitionHigh


1 Answers

Just do this.

private void button1_Click(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem((o) =>
             {
                 Dispatcher.Invoke((Action) (() => info.Text = "step 1"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 2"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 3"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 4"));
                 wait(1000);
             });
    }
like image 86
Dean Chalk Avatar answered Jan 08 '23 05:01

Dean Chalk