470,619 Members | 1,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

AppDomain.UnhandledException in Windows Forms and Windows Service

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)
'Code to start service here
Catch ex As Exception
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,

Why doesn't the UnhandledException handler work? Why is it never



Nov 21 '05 #1
2 6320
stand__sure wrote:
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
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

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

Nov 21 '05 #2
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Marcio Izepe | last post: by
reply views Thread by kozen | last post: by
1 post views Thread by Kimmo Laine | last post: by
5 posts views Thread by John Richardson | last post: by
2 posts views Thread by guy | last post: by
reply views Thread by Zeya | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.