【发布时间】:2015-03-05 20:53:50
【问题描述】:
我正在编写一个每 10 或 15 分钟执行一次操作的程序。我希望它一直在运行,所以我需要一些处理能力便宜的东西。到目前为止,我所阅读的内容似乎表明我想使用计时器。这是我到目前为止的代码片段。
class Program {
private static Timer timer = new Timer();
static void Main(string[] args) {
timer.Elapsed += new ElapsedEventHandler(DoSomething);
while(true) {
timer.Interval = TimerMilliseconds(); // The duration of the wait will differ each time
timer.Enabled=true;
}
}
}
这里的问题是 while 循环一直在快速执行。如何在计时器结束之前暂停执行。我的程序真的不需要多线程。计时器是否适合这项工作?
提前感谢您的帮助!
更新:很抱歉造成混乱。我已经实现了 DoSomething 方法。我只是没有包括它,因为我不认为这是我的问题的一部分。
【问题讨论】:
-
提示:删除你的while循环并实现
DoSomething。 -
此外,您还可以考虑将您的应用程序设为Windows Service
标签: c#