AppDomain.UnhandledException in Windows Forms and Windows Service

C

Chris Dunaway

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
 
S

stand__sure

from <http://msdn2.microsoft.com/library/d7s55hcw(en-us,vs.80).aspx>

"An UnhandledExceptionEventHandler can only be specified for the
default application domain that is created by the system to execute an
application."

I *suspect* that this is the cause of the problem -- the SCM may be the
default app domain.

are you re-throwing the error in your handler? I ask b/c as presented
in your OP, the error is handled (by you) and thus will not crash the
app
 
C

Chris Dunaway

stand__sure said:
from <http://msdn2.microsoft.com/library/d7s55hcw(en-us,vs.80).aspx>

"An UnhandledExceptionEventHandler can only be specified for the
default application domain that is created by the system to execute an
application."

My UnhandledExceptionHandler is not called even in a standard Windows
Forms app. It *is* called if the app is run through the IDE but it *is
not* called when run outside the IDE.
are you re-throwing the error in your handler? I ask b/c as presented
in your OP, the error is handled (by you) and thus will not crash the
app

The Try/Catch in the code I posted was my attempt to call the
UnhandledExceptionHandler code. I should not have to call this handler
myself. There is something wrong, I feel, in the way that handler is
handled.
 
S

stand__sure

This may offer some clues (and came as a bit of a surprise to me)

from <http://msdn.microsoft.com/msdnmag/issues/04/06/NET/>

Here is a summary of the unhandled exception handler default behaviors
that are executing in this application.

* Unhandled exceptions that occur on the application's main thread
cause the application to terminate.
* Unhandled exceptions that occur in threads other than the
application's main thread are swallowed by the CLR. This includes
manual threads, thread pool threads, and the CLR's finalizer thread. If
your application is a console application, the CLR outputs exception
text to the console (though your application keeps running). If your
application is not a console application, there is no outward
indication when an exception occurs and your application keeps running.
* Unhandled exceptions that occur on a thread that is pumping
window messages via the Windows Forms classes are subject to the
Windows Forms unhandled exception handler. A debug dialog box is
produced by default, but this behavior can be overridden (more on this
in a moment).

***

there was also apparently a discussion on this issue wrt async
callbacks which shows adding this event handler to
Application.ThreadException
<http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic4943.aspx>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top