我在窗口服务中创建了一个计时器,它在系统启动后运行。我想在特定时间启动它,让我们说:3:00 PM ..
这是我试过的
private Timer scheduleTimer = null;
private DateTime lastRun;
private bool flag;
public AutoSMSService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("AutoSMSSource"))
{ System.Diagnostics.EventLog.CreateEventSource("AutoSMSSource", "AutoSMSLog"); }
eventLogAutoSMS.Source = "AutoSMSSource";
eventLogAutoSMS.Log = "AutoSMSLog";
scheduleTimer = new Timer();
scheduleTimer.Interval = 5000;
}
protected override void OnStart(string[] args)
{
flag = true;
lastRun = DateTime.Now;
scheduleTimer.Start();
//some operation
}
protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (flag == true)
{
lastRun = DateTime.Now;
flag = false;
}
else if (flag == false)
{
if (lastRun.Date < DateTime.Now.Date)
{
eventLogAutoSMS.WriteEntry("DB Call after Interval");
ASMSFetch.Program.UpdateSMS();
}
}
}
protected override void OnStop()
{
eventLogAutoSMS.WriteEntry("Stopped");
}
在类似的帖子上..没有提到如何在特定的日子设置它.. 任何建议都会有所帮助