Time delay in For loop in c#

前端 未结 7 1295
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 01:18

how can i use a time delay in a loop after certain rotation? Suppose:

for(int i = 0 ; i<64;i++)
{
........
}

i want 1 sec delay after ea

相关标签:
7条回答
  • 2020-12-06 02:19

    Once I needed to write a program witch was so sensitive and needed accurate timing (about Nano seconds).

    I used all sorts of stuff like Thread.Sleep(), Timer and... but none of them was that accurate that could answer my needs so I used this code for delaying:

    var stopWatch = new Stopwatch();
    for(int i = 0 ; i<64;i++)
    {
    stopWatch.Restart();
    while (stopWatch.ElapsedMilliseconds <= 1000)
    {
    
    }
    }
    

    Of course this way is not very CPU friendly, yet when you need accuracy that much you have to go old school.

    0 讨论(0)
提交回复
热议问题