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

UnhandledException in Windows Service

P: n/a
I can't seem to get a custom UnhandledException handler working for a
Windows Service I'm writing in VB.NET. I've read the MSDN and tried
various different placements of the AddHandler call, to no avail.

Here's the code I'm using, all of which is in the main service class:
Protected Overrides Sub OnStart(ByVal args() As String)

AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler
Throw New Exception

End Sub

Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal e As System.UnhandledExceptionEventArgs)

Me.EventLog.WriteEntry("This entry will never be written, " & _
"because the programmer doesn't know what he's doing.")

End Sub

Any help would be appreciated, so if you want to be appreciated by an
idiot, please help.

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


P: n/a
Groundskeeper,
AppDomain.UnhandledException only handles unhandled exceptions.

The OnStart routine has Try/Catch around it, hence the
UnhandledExceptionHandler is not called.

You can tell that OnStart has a Try/Catch around it, because if OnStart
throws an exception the service remains at in the Stop state, with an error
that is automatically logged.

Note its common to directly or indirectly to use Asynchronous Delegates in a
Windows Service, AppDomain.UnhandledException is not used for Asynchronous
Delegates either, you need to be certain to use end EndInvoke with
Asynchronous & catch the exception there, with possible logging. As far as I
can tell the System.Timers.Timer.Elapsed event is called via an Asynchronous
Delegate, which means you should wrap the Elapsed event handler in a
Try/Catch so as not to loose the Exception.

As far as I can tell ThreadPool.QueueUserWorkItem uses
AppDomain.UnhandledException so you should be safe there. This goes for
System.Threading.Timer also.

Hope this helps
Jay

"Groundskeeper" <jf******@austin.rr.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I can't seem to get a custom UnhandledException handler working for a
Windows Service I'm writing in VB.NET. I've read the MSDN and tried
various different placements of the AddHandler call, to no avail.

Here's the code I'm using, all of which is in the main service class:
Protected Overrides Sub OnStart(ByVal args() As String)

AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler
Throw New Exception

End Sub

Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal e As System.UnhandledExceptionEventArgs)

Me.EventLog.WriteEntry("This entry will never be written, " & _
"because the programmer doesn't know what he's doing.")

End Sub

Any help would be appreciated, so if you want to be appreciated by an
idiot, please help.

Nov 21 '05 #2

P: n/a
That helps, thanks.

The service in question runs a timer (System.Timers.Timer) to watch for
files to process. (Earlier versions had used the filesystemwatcher, but
ran in to problems with it, but that's another story.) From what you
say, it sounds like simply wrapping the code in the timer's Elapsed
event and the OnStart and OnStop service events in Try/Catch blocks
should catch just about anything I can hope to catch.

Out of curiosity, is it only through trial and error that one can
discover which procedures can be expected to raise errors to a global
handler for unhandled exceptions? Put another way, when is adding a
handler for unhandled exceptions useful/needed in a Windows Service?

Jay B. Harlow [MVP - Outlook] wrote:
Groundskeeper,
AppDomain.UnhandledException only handles unhandled exceptions.

The OnStart routine has Try/Catch around it, hence the
UnhandledExceptionHandler is not called.

You can tell that OnStart has a Try/Catch around it, because if OnStart throws an exception the service remains at in the Stop state, with an error that is automatically logged.

Note its common to directly or indirectly to use Asynchronous Delegates in a Windows Service, AppDomain.UnhandledException is not used for Asynchronous Delegates either, you need to be certain to use end EndInvoke with
Asynchronous & catch the exception there, with possible logging. As far as I can tell the System.Timers.Timer.Elapsed event is called via an Asynchronous Delegate, which means you should wrap the Elapsed event handler in a
Try/Catch so as not to loose the Exception.

As far as I can tell ThreadPool.QueueUserWorkItem uses
AppDomain.UnhandledException so you should be safe there. This goes for System.Threading.Timer also.

Hope this helps
Jay

"Groundskeeper" <jf******@austin.rr.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I can't seem to get a custom UnhandledException handler working for a Windows Service I'm writing in VB.NET. I've read the MSDN and tried
various different placements of the AddHandler call, to no avail.

Here's the code I'm using, all of which is in the main service class:

Protected Overrides Sub OnStart(ByVal args() As String)

AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf UnhandledExceptionHandler
Throw New Exception

End Sub

Sub UnhandledExceptionHandler(ByVal sender As Object, _
ByVal e As System.UnhandledExceptionEventArgs)

Me.EventLog.WriteEntry("This entry will never be written, " & _
"because the programmer doesn't know what he's doing.")

End Sub

Any help would be appreciated, so if you want to be appreciated by an idiot, please help.


Nov 21 '05 #3

P: n/a
Groundskeeper,
say, it sounds like simply wrapping the code in the timer's Elapsed
event and the OnStart and OnStop service events in Try/Catch blocks I am saying wrap the Timer's Elapsed event in a Try/Catch block.

The OnStart & OnStop are already wrapped in Try/Catch blocks! (inside of the
ServiceBase class)
Out of curiosity, is it only through trial and error that one can
discover which procedures can be expected to raise errors to a global Basically that is it & reading the documentation
(http://msdn.microsoft.com/library).
Put another way, when is adding a
handler for unhandled exceptions useful/needed in a Windows Service? When you are using ThreadPool.QueueUserWorkItem & System.Threading.Timer.

My observations are based on .NET 1.1 (VS.NET 2003) things may very well
change in .NET 2.0 (VS.NET 2005 aka Whidbey, due out later in 2005) & .NET
1.0.

Hope this helps
Jay

"Groundskeeper" <jf******@austin.rr.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... That helps, thanks.

The service in question runs a timer (System.Timers.Timer) to watch for
files to process. (Earlier versions had used the filesystemwatcher, but
ran in to problems with it, but that's another story.) From what you
say, it sounds like simply wrapping the code in the timer's Elapsed
event and the OnStart and OnStop service events in Try/Catch blocks
should catch just about anything I can hope to catch.

Out of curiosity, is it only through trial and error that one can
discover which procedures can be expected to raise errors to a global
handler for unhandled exceptions? Put another way, when is adding a
handler for unhandled exceptions useful/needed in a Windows Service?

Jay B. Harlow [MVP - Outlook] wrote:
Groundskeeper,
AppDomain.UnhandledException only handles unhandled exceptions.

The OnStart routine has Try/Catch around it, hence the
UnhandledExceptionHandler is not called.

You can tell that OnStart has a Try/Catch around it, because if

OnStart
throws an exception the service remains at in the Stop state, with an

error
that is automatically logged.

Note its common to directly or indirectly to use Asynchronous

Delegates in a
Windows Service, AppDomain.UnhandledException is not used for

Asynchronous
Delegates either, you need to be certain to use end EndInvoke with
Asynchronous & catch the exception there, with possible logging. As

far as I
can tell the System.Timers.Timer.Elapsed event is called via an

Asynchronous
Delegate, which means you should wrap the Elapsed event handler in a
Try/Catch so as not to loose the Exception.

As far as I can tell ThreadPool.QueueUserWorkItem uses
AppDomain.UnhandledException so you should be safe there. This goes

for
System.Threading.Timer also.

Hope this helps
Jay

"Groundskeeper" <jf******@austin.rr.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
>I can't seem to get a custom UnhandledException handler working for a > Windows Service I'm writing in VB.NET. I've read the MSDN and tried
> various different placements of the AddHandler call, to no avail.
>
> Here's the code I'm using, all of which is in the main service class: >
>
> Protected Overrides Sub OnStart(ByVal args() As String)
>
> AddHandler AppDomain.CurrentDomain.UnhandledException, _
> AddressOf UnhandledExceptionHandler
> Throw New Exception
>
> End Sub
>
> Sub UnhandledExceptionHandler(ByVal sender As Object, _
> ByVal e As System.UnhandledExceptionEventArgs)
>
> Me.EventLog.WriteEntry("This entry will never be written, " & _
> "because the programmer doesn't know what he's doing.")
>
> End Sub
>
>
>
> Any help would be appreciated, so if you want to be appreciated by an > idiot, please help.
>

Nov 21 '05 #4

P: n/a
I really appreciate your replies. Sorry I didn't reply more recently;
I'm new to Google groups and had expected an email notifying me of any
activity on this thread.

What I meant by suggesting that OnStart and OnStop should have
Try/Catch blocks inside them is that this way, I can catch the errors
in my code and handle them myself, rather than relying on the default
handling in ServiceBase.

Once again, this idiot appreciates your help.

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.