473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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)
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
2 6497
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marcio Izepe | last post by:
I am with a problem that I do not obtain to understand. I put a method in the UnhandledException event of AppDomain to capture and log exceptions, however it does not pass for it when I am working...
2
by: Martin Lapierre | last post by:
How can I remove the default exception handler handler? When adding a custom handler, I can't get rid of the VS.NET unhandled exception dialog. Ex: AppDomain currentDomain =...
0
by: kozen | last post by:
I want to pass a WinForm control from main appDomain A to another appDomain B. In appDomain B, i will create some WinForm controls and add them to the control's child controls list of main...
1
by: Kimmo Laine | last post by:
Hi, does the AppDomain.UnhandledException also work when you use it in Windows Service - i was unable to get the handler called when i generated an exception. thx Kimmo Laine
5
by: John Richardson | last post by:
Quick question about the UnhandledException event and associated Handler. I just implemented this handler for the first time, and am surprised that it this event is being raised for an exception...
2
by: guy | last post by:
In my application I have an event handler to catch unhandled exceptions, as per the MSDN documentation, however when an unhandled exception is thrown I get the Dialogue first, then the event...
1
by: pack | last post by:
What're the meaning of "App domain" and "Current Domain" in appdomain.CurrentDomain.UnhandledException?
0
by: Zeya | last post by:
Situation: Using C#, ASP.Net Requirement: 1. ASP.net application with virtual hosting service. 2. Requires a service that will run every predefined frequency in minutes (2, 30, 100, 10000)...
1
by: José Joye | last post by:
I'm currently trying to load an instance of a given class within a secondary appDomain and access it from within my main AppDomain. Everything is fine and working if the class in the second...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.