I want to know how to refresh a wpf element before Thread.sleep() is executed. In the scenario below the Thread.sleep() call is executed first, then update the wpf element.
I tried this:
In the button_click event handler:
EDIT: To understand what I want, I added some variable assignations and comments.
private void button_click(object sender, RoutedEventArgs e){
//fist of all, set the static variable to_change to true, then Update GUI label
to_change = true;
Thread thread = new Thread(UpdateGUI);
thread.Start();
//Then sleep 1 second and ( With label changed)
Thread.Sleep(1000);// one second
//and lately reset the value of to_change to false and update the GUI again
to_change = false;
//UpdateGUI.
}
And in UpdateGUI I have:
private void UpdateGUI()
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(ThreadStart)delegate()
{
this.label_success.Content = "Successful!";
}
);
}
Also I’ve tried with DispatcherPriority.Send which is the highest priority
I guess i’m missing some important concept.
Thanks in advance !
You can force WPF to process all of the items in its queue by Invoking to the Dispatcher an item that is a lower or equal priority to the task you want to execute. Just pass an empty delegate as the action, like this.
This will work; however doing this should not be “normal”, and I’m curious why you need to sleep your main UI thread… The code you show above is scary, but I don’t know the big picture, so maybe there is a reason for this?