2014-02-24 16 views
1

我在Windows服务中使用System.Threading.Timer来执行例行任务。未处理的回调方法中抛出的异常会使服务崩溃吗?我在网上搜索,找不到任何有用的链接。定时器回调中未处理的异常

我在事件日志中发现这个异常,服务崩溃。

at MyNameSpace.MyClass.MyCallback(System.Object) 
at System.Threading.ExecutionContext.runTryCode(System.Object) 
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object) 
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
at System.Threading._TimerCallback.PerformTimerCallback(System.Object) 

任何关于此的MSDN文章指针将不胜感激。

+0

这是你自己可以找到的东西。在回调中抛出异常并查看会发生什么。 – Marcom

+0

在回调方法中抛出异常(而未捕获)将发生在另一个线程上。根据你的应用程序的写法,它可能会导致你的服务崩溃。但是,由于回调不在主线程中,因此可能会将异常吞下或写入事件查看器。 可能需要发布一些代码供其他人更详细地回答。 或搜索http://stackoverflow.com/questions/1718598/throwing-exceptions-in-callback-method-for-timers – MattC

+0

在未处理的异常情况下不会崩溃服务的任何理由? – Dennis

回答

0

从异常处理的角度来看,服务的行为与任何其他应用程序相似。
对于执行定时器回调的工作线程也是如此。

如果异常未处理,没有理由继续应用。该服务将被终止。