Implementing a loop using a timer in C#

后端 未结 4 1131
终归单人心
终归单人心 2021-01-11 22:23

I wanted to replace a counter based while loop with the timer based while loop in C#.

Example :

while(count < 100000)
{
   //do something 
}
         


        
4条回答
  •  無奈伤痛
    2021-01-11 23:25

    What about using the Stopwatch class.

    using System.Diagnostics;
    //...
    Stopwatch timer = new Stopwatch();
    timer.Start();
    while(timer.Elapsed.TotalSeconds < Xseconds)
    {
        // do something
    }
    timer.Stop();
    

提交回复
热议问题