Fire timer_elapsed immediately from OnStart in windows service

前端 未结 4 1408
情话喂你
情话喂你 2021-02-13 20:33

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(         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-13 21:29

    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.

提交回复
热议问题