C#定时器(减速循环) [英] C# timer (slowing down a loop)
本文介绍了C#定时器(减速循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想慢下来一个循环,使其循环每隔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).
这篇关于C#定时器(减速循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文