Can I use a timer to update a label every x milliseconds

前端 未结 4 1038
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 10:32

This is my code:

Stopwatch timer = new Stopwatch();
timer.Start();
while (timer.ElapsedMilliseconds < 3000) {
    label1.Text = Convert.ToString( timer.El         


        
4条回答
  •  走了就别回头了
    2021-01-20 11:16

    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 Timer and update the UI periodically when the timer event fires; every 50ms would be the absolute fastest I'd go, personally.

提交回复
热议问题