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
}
好主意,谢谢 – user1438082