2014-04-13 27 views
0

单独的线程我有一个窗口服务,在启动方法,创建一个定时器,触发计时器立即执行。计时器是一个长时间运行的任务,因此services.msc启动时的服务会被检测为错误。它认为下面的计时器运行在一个单独的线程中,服务应该立即启动?定时器与长期运行的任务

如果我删除线下它工作正常,但我想,一旦服务启动服务来触发。

_timer_Elapsed(null, null); // Remiving this line makes the issue go away but i want this. 


protected override void OnStart(string[] args) 
     { 

      _timer = new System.Timers.Timer(); 
      _timer.AutoReset = false; 
      _timer.Interval = (Convert.ToInt32(ConfigurationManager.AppSettings["CheckInterval"])); 
      _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed); 
      _timer.Enabled = true; 
      _timer.Start(); // Get the timer to execute immediately 
      _timer_Elapsed(null, null); // Get the timer to execute immediately 

     } 

回答

1
_timer_Elapsed(null, null); // Get the timer to execute immediately 

这不会立刻触发计时器。它所做的只是执行定时器设置为执行的相同过程。只是在OnStart的线程。

如果你希望计时器立刻触发,时间间隔设置为1点或小东西第一轮。

+0

好主意,谢谢 – user1438082