By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,706 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,706 IT Pros & Developers. It's quick & easy.

AppDomain.UnhandledException in Windows Forms and Windows Service

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
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.

Nov 21 '05 #2

P: n/a
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>

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.