This is my code:
Stopwatch timer = new Stopwatch();
timer.Start();
while (timer.ElapsedMilliseconds < 3000) {
label1.Text = Convert.ToString( timer.ElapsedMilliseconds );
}
timer.Stop();
My intetion was to update the label’s text in real time, so if timer.ElapsedMilliseconds == 1350, then label1.Text = 1350. How can I do this? Thanks in advance!
You cannot update the UI in a tight loop like that, because while the UI thread is running that code, it isn’t responding to paint events. You can do nasty things like “DoEvents()”, but please don’t… it would be better to just have a
Timerand update the UI periodically when the timer event fires; every 50ms would be the absolute fastest I’d go, personally.