C#定时器(减速循环)定时器

2023-09-03 03:55:44 作者:喝旺仔旳小女生

我想慢下来一个循环,使其循环每隔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).