我已经在特定时间看到过设置计时器的类似帖子...我不想运行计时器整天...我想在特定时间启动它.. 大部分的建议是使用计划任务......但我想和窗口服务做到这一点....窗口服务:如何在特定时间启动计时器
这是我服务的工作代码:
public AutoSMSService2()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
Timer checkForTime = new Timer(5000);
checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
checkForTime.Enabled = true;
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
eventLog1.WriteEntry("Timer Entry");
}
我的计时器是工作的罚款,并添加日志间隔5秒..但我想开始计时器让我们说下午3点...
private static void SetTimer(Timer timer, DateTime due)
{
var ts = due - DateTime.Now;
timer.Interval = ts.TotalMilliseconds;
timer.AutoReset = false;
timer.Start();
}
但我不知道如何实现它的代码..
任何建议将是有益的
只需检查已过期的处理程序中的“DateTime.Now”,看它是否在下午3点之后。如果是,写日志,如果没有,则什么也不做。 –
其实我不想在每个5分钟的间隔运行计时器..我想在特定的时间启动它 –
你不能,你要么检查它每隔一段时间或你安排它。没有办法让它知道时间现在是3PM –