Fire timer_elapsed immediately from OnStart in windows service

前端 未结 4 1415
情话喂你
情话喂你 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条回答
  •  旧时难觅i
    2021-02-13 21:23

    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...
        }
    

提交回复
热议问题