Fire timer_elapsed immediately from OnStart in windows service

喜夏-厌秋 提交于 2019-12-04 19:16:15

问题


I'm using a System.Timers.Timer and I've got code like the following in my OnStart method in a c# windows service.

timer = new Timer();
timer.Elapsed += timer_Elapsed;
timer.Enabled = true;
timer.Interval = 3600000;
timer.Start();

This causes the code in timer_Elapsed to be executed every hour starting from an hour after I start the service. Is there any way to get it to execute at the point at which I start the service and then every hour subsequently?

The method called by timer_Elapsed takes too long to run to call it directly from OnStart.


回答1:


Just start a threadpool thread to call the worker function, just like Timer does. Like this:

        timer.Elapsed += timer_Elapsed;
        ThreadPool.QueueUserWorkItem((_) => DoWork());
    ...

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
        DoWork();
    }

    void DoWork() {
        // etc...
    }



回答2:


Use AutoReset Property of System.Timers.Timer and set it value to "true". No need to use timer.Start() because it does the same job as timer.Enabled = true;

timer = new Timer();
timer.Elapsed += timer_Elapsed;
timer.Enabled = true;
timer.Interval = 3600000;
timer.AutoReset = true;

AutoReset = true will set a value indicating that the Timer should raise the Elapsed event each time when the specified interval elapses.




回答3:


Use System.Threading.Timer class instead of System.Timers.Timer as this type is just a wrapper for Threading Timer.

It also suits your requirement.

 System.Threading.Timer timer =
                new System.Threading.Timer(this.DoWork, this, 0, 36000);

Here are the details.




回答4:


If you want your Timer to be fired immediately then you could simply just initialize the Timer object without a specified interval (it will default to 100ms which is almost immediately :P), then set the interval within the called function to whatever you like. Here is an example of what I use in my Windows Service:

private static Timer _timer;

protected override void OnStart(string[] args)
{
    _timer = new Timer(); //This will set the default interval
    _timer.AutoReset = false;
    _timer.Elapsed = OnTimer;
    _timer.Start();
}

private void OnTimer(object sender, ElapsedEventArgs args)
{
    //Do some work here
    _timer.Stop();
    _timer.Interval = 3600000; //Set your new interval here
    _timer.Start();
}


来源:https://stackoverflow.com/questions/5742754/fire-timer-elapsed-immediately-from-onstart-in-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!