C# countdown timer

后端 未结 4 1178
有刺的猬
有刺的猬 2020-12-16 01:39

I\'m trying to make a countdown using C# and show the time in format:

hour:minutes:seconds

I\'ve tried this:

 var minutes          


        
4条回答
  •  庸人自扰
    2020-12-16 02:18

    Your code sets up the variables then goes to sleep for 3 minutes so the if-statement isn't executed until it leaves the sleep state. Either set up a new thread to update the UI or do something like this...

    while (DateTime.now < end) {
      label1.Text = "...";
      Thread.Sleep(#); //Pick a second, or 5 or whatever
    }
    
    label1.Text = "Done!";
    

    With a 2nd thread you can still do stuff in your program while it works. "Done!" will appear once it finishes.

提交回复
热议问题