Whenever I update contents of a label from inside a loop, the changes are no instantly replicated but only the final value comes up in the end. How can I force it to replicate the changes instantly?
for(int i=0; i<5; i++) {
label1.Content = x[i];
Thread.sleep(100);
}
I want it to change the label value five times while waiting for 100 ms in between.
If you’re doing this on the main UI thread (which you are unless you’ve started your own thread) the thread won’t be free to update the UI until after your loop is finished. Use a
DispatcherTimerinstead and register a handler for theTickevent and update the label in the event handler. That way you won’t lock up the UI thread for the duration of the loop (which you are doing when you are callingThread.Sleep).