我想慢下来一个循环,使其循环每隔5秒。
I would like to slow down a loop so that it loops every 5 seconds.
在动作,我会用一个定时器和一个计时器完整的事件来做到这一点。我将如何在C#中去的呢?
In ActionScript, I would use a timer and a timer complete event to do this. How would I go about it in C#?
您可以添加这个调用您的循环中:
You can add this call inside your loop:
System.Threading.Thread.Sleep(5000); // 5,000 ms
或preferable更好的可读性:
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).