I am attempting to use the AppDomain.UnhandledException event in a
Windows Forms app and also in a Windows Service. But the event doesn't
seem to be called.
In a Windows Forms app, the event IS called but only if I run the app
through the IDE. If run standalone (release or debug build, it doesn't
matter), the event handler is never called. In the Windows Forms app,
I changed it to use the Application.ThreadException event and that
seems to work fine.
But I cannot get it to work in a Windows Service app. the
AppDomain.UnhandledException event handler is never called in a Windows
Service. And I cannot use Application.ThreadException since that is
part of the Windows.Forms namespace. How do you deal with unhandled
exceptions in a Windows service?
In the OnStart method I tried something like this:
Protected Overrides Sub OnStart(ByVal args() As String)
Try
'Code to start service here
Catch ex As Exception
UnhandledExceptionHandler(ex)
End Try
End Sub
Where UnhandledExceptionHandler is just a sub with some logging
actions. This seems to catch the error, but at the same time, the
service does not exit as it should. The Service Control Manager shows
that the service has started!! If I *don't* handle the exception, the
service stops as it should but I cannot perform any custom logging,
etc.
Why doesn't the UnhandledException handler work? Why is it never
called?
Thanks,
Chris