相关疑难解决方法(0)

如何在Windows服务中设置.NET UnhandledException处理?

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服务中设置未处理的异常处理的正确方法吗?

c# windows-services exception-handling unhandled-exception

47
推荐指数
5
解决办法
3万
查看次数