protected override void OnStart(string[] args)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Thread.Sleep(10000);
throw new Exception();
}
void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)
我在我的Windows服务中将调试器附加到上面的代码,在CurrentDomain_UnhandledException中设置断点,但它从未被命中.弹出的异常是说它未处理,然后服务停止.我甚至尝试在事件处理程序中放入一些代码,以防它被优化掉.
这不是在Windows服务中设置未处理的异常处理的正确方法吗?