Reputation: 13953
private void Window_ContentRendered_1(object sender, EventArgs e)
{
StatusLabel.Text = "Doesnt show up until the sleep is over...0_0";
System.Threading.Thread.Sleep(2000);
}
I want to ask why does it heppend and only then a solution. thanks all
Upvotes: 0
Views: 1434
Reputation: 81253
Issue is you are sleeping on UI thread.
After first line execution you slept on UI thread but it's UI thread responsibility to refresh UI (UI thread refreshes UI on Render dispatcher priority). Hence, you see no update after first line execution because UI thread never gets time to execute render priority items queued on dispatcher.
Ideally, you should use DispatcherTimer to wait on UI thread for certain interval of time and then update once interval time is elapsed.
Also, either you should move the long running task on background thread using backgroundWorker and sleep on that thread instead on UI thread. Also as SLaks suggested you can use async/await to get desired result.
However, quick and dirty solution would be to dispatch empty delegate on UI thread with Dispatcher priority set to Render just before you slept on it so that UI refresh part gets executed before thread falls in sleep state.
StatusLabel.Text = "Doesnt show up until the sleep is over...0_0";
lbStatus.Dispatcher.Invoke((Action)(() => { }), DispatcherPriority.Render);
Thread.Sleep(2000);
Idea here is once you queue empty delegate on UI dispatcher with render priority, it will execute all pending items with priority higher or equal to Render. Since, UI refresh is done on on Render priority, UI will be refreshed and you will see update in label content on GUI.
Upvotes: 3