C# timer (slowing down a loop)

后端 未结 4 2045
我寻月下人不归
我寻月下人不归 2020-12-19 02:58

I would like to slow down a loop so that it loops every 5 seconds.

In ActionScript, I would use a timer and a timer complete event to do this. How would I go about i

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 03:41

    You can add this call inside your loop:

    System.Threading.Thread.Sleep(5000); // 5,000 ms
    

    or preferable for better readability:

    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));
    

    However, if your application has a user interface you should never sleep on the foreground thread (the thread that processes the applications message loop).

提交回复
热议问题