How can I set up .NET UnhandledException handling in a Windows service?

Posted by Mike Pateras on Stack Overflow See other posts from Stack Overflow or by Mike Pateras
Published on 2010-03-16T17:54:52Z Indexed on 2010/03/28 20:43 UTC
Read the original article Hit count: 559

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)
{
}

I attached a debugger to the above code in my windows service, setting a breakpoint in CurrentDomain_UnhandledException, but it was never hit. The exception pops up saying that it is unhandled, and then the service stops. I even tried putting some code in the event handler, in case it was getting optimized away.

Is this not the proper way to set up unhandled exception handling in a windows service?

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-services