【发布时间】:2014-06-13 04:24:38
【问题描述】:
我在窗口服务中创建了一个计时器,它在系统启动后运行..我想在特定时间启动它,比如说:下午 3:00 ..
这是我尝试过的
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");
}
在类似的帖子上.. 没有提到如何在特定日期设置它.. 任何建议都会有所帮助
【问题讨论】:
-
您使用的是哪个 Timer 类? System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer
-
我正在使用 System.Timers.Timer
-
如何将间隔设置为 1 分钟。在您过去的事件中,您可以检查当前时间是否为 3:00。如果不是 3:00,则返回。
-
您的意思是要在特定时间启动计时器?
-
是的 .. 我的意思是你想在特定时间启动计时器