我开发了使用System.Threading.Timer的Windows服务。计时器每隔x分钟启动一次,并且工作正常(计时器在方法结束时更新)。但是,如果尝试阻止服务中存在错误,则尽管事实是我正在更新计时器并告诉他何时重新开始 为什么会发生这种情况?这里是代码:如果出现异常,Windows服务将停止
System.Threading.Timer serviceTimer;
protected override void OnStart(string[] args)
{
TimeSpan diff;
diff = nextRun - now;
TimerCallback timerDelegate =
new TimerCallback(MyTimerCallback);
serviceTimer = new System.Threading.Timer(timerDelegate, null,
diff, new TimeSpan(-1));
}
public void MyTimerCallback(object something)
{
try
{
//possible error that happened
}
catch(Exception)
{
}
finally
{
//diff is a new variable telling timer when to start again
serviceTimer.Change(diff, new TimeSpan(-1));
}
}
我错过了为什么服务停止,如果有错误?
什么是你在'catch'阻止在做什么?尝试块中的 – Oded
我正在调用Web服务的方法: var result = proxy.ProxyMethod(param,param); 因此,如果服务不可用,可能会出错 – Avicena00
如果出现这样的错误,您的服务_will_将停止并出现未处理的异常。 – Oded