且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows服务每隔一小时运行一次

更新时间:2023-12-02 16:14:52

使用 System.Timers.Timer 您有更多选项,其中 System.Threading.Timer 是一个轻量级计时器。我建议你使用 System.Timers.Timer



试试这个...



With System.Timers.Timer you have more options, where System.Threading.Timer is a lightweight timer. I would recommend you to use System.Timers.Timer

Try this ...

using System.Timers;

Timer tmrExecutor = new Timer();

protected override void OnStart(string[] args)
{
      tmrExecutor.Elapsed += new ElapsedEventHandler(tmrExecutor_Elapsed); // adding Event
      tmrExecutor.Interval = 5000; // Set your time here 
      tmrExecutor.Enabled = true;
      tmrExecutor.Start();
}

private void tmrExecutor_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
      //Do your work here 
}

protected override void OnStop()
{
      tmrExecutor.Enabled = false;
}