我想写一个Windows服务。但它需要在特定时间运行。例如,我的服务应每5分钟给我发一封电子邮件。如何让我的Windows服务以特定间隔运行?
private Timer _timer;
private DateTime _lastRun = DateTime.Now;
protected override void OnStart(string[] args)
{
_timer = new Timer(10 * 60 * 1000); // every 10 minutes
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
//...
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// ignore the time, just compare the date
if (_lastRun.Date < DateTime.Now.Date)
{
// stop the timer while we are running the cleanup task
_timer.Stop();
//
// do cleanup stuff
//
_lastRun = DateTime.Now;
_timer.Start();
}
}
我希望我的服务每5分钟给我发一封电子邮件。
我不认为这是足够的信息是诚实的。真的很难提供任何有用的代码。 [试试这个](http://stackoverflow.com/questions/503564/how-might-i-schedule-ac-sharp-windows-service-to-perform-a-task-daily) – yoonsi 2012-07-16 08:53:33
这个问题已被问到,在这里是链接。 您使用此System.Threading.Timer这是成功的关键 http://stackoverflow.com/questions/246697/windows-service-and-timer – IamStalker 2012-07-16 08:55:43
我想给我发邮件,ı编辑我的问题 – 2012-07-16 08:55:52