【发布时间】:2017-06-06 11:10:58
【问题描述】:
我的问题更多是关于到达同一个目的地,但必须有另一种方式。现在我正在创建一个DateTime 并将其与另一个DateTime 进行比较,然后检查我设置的时差是否正确。到目前为止一切顺利,但我不能接受每次循环进入该代码时我都会创建一个新属性。
是否有任何可能的方式可以到达同一个目的地,但以某种更有效的方式?
我在这里为你们提供了一些示例代码:
private void RunService()
{
// Runs as long as the service didn't got a stop call.
while (!SetStop)
{
//Get MinutesToWait
this.MinutesToWait = 5;
DateTime CheckRunTime = this.LastRun;
CheckRunTime.AddMinutes(this.MinutesToWait);
if (DateTime.Now >= CheckRunTime)
{
// Imagine some good and smart and totally runnable code?
}
}
}
【问题讨论】:
-
你可以使用定时器,msdn.microsoft.com/en-us/library/…
-
您发现
System.Threading.Timer对象了吗?但老实说,我会强烈敦促您重新考虑。 "If you're writing a Windows Service that runs a timer, you should re-evaluate your solution." 计划任务会更合适。 -
@CodyGray 这是一篇非常有趣的博文。我应该考虑使用计划任务来完成它......但我仍然对解决方案感兴趣。
标签: c# .net windows-services