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

前端 未结 4 1090
佛祖请我去吃肉
佛祖请我去吃肉 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:17

    Is this a WinForms app?

    The problem is that while your loop runs, it does not give any other tasks (like updating the GUI) any possibility to get done, so the GUI will update the the entire loop is complete.

    You can add a quick and "dirty" solution here (if WinForms). Modify your loop like this:

    while (timer.ElapsedMilliseconds < 3000) {
      label1.Text = Convert.ToString( timer.ElapsedMilliseconds );
      Application.DoEvents();
    }
    

    Now the label should update in between the loop runs.

提交回复
热议问题