Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global threadexception for Windows Service?

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
I know that you can make a global exception for an application by adding a handler like this:

AddHandler Application.ThreadException, AddressOf Application_ThreadException

But what about windows services? Can you catch global errors there?

thanks!

--- neteject.com - Internet Solutions ---
 
The Application object is part of the Windows Forms namespace, and since services aren't supposed to have user interfaces, you probably shouldn't be using the ThreadException handler.

It's really a bad name -- You would think it comes from System.Threading, but it doesn't. You'll probably want to use the AppDomain.UnhandledException handler instead.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I tried adding (at different places, new, start) in the service:

AddHandler AppDomain.CurrentDomain.UnhandledException, New System.UnhandledExceptionEventHandler(AddressOf ThreadException)

Here is my function:

Public Sub ThreadException(ByVal sender As Object, ByVal e As System.UnhandledExceptionEventArgs)
Dim ex As Exception = CType(e.ExceptionObject, Exception)
MsgBox(ex.ToString)
End Sub

( I use desktop interactive mode, so I should see the messagebox )

Somewhere, in the start a I call a function which does this:

Dim i As Double = CType("sdjlksfd", Double)

This makes the service start and then stop, but I don't see a messagebox with the error. I have tested so that I can see other messageboxes.

Do you have any ideas how to do this "right"?

--- neteject.com - Internet Solutions ---
 
Re-read my post, especially the part about services not having user interfaces.

You'll need to write any error messages to a log file, or the event viewer.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Don't know exactly what you mean, I have no problem showing a messagebox from a service (when it's running in interactive mode).

Anyway, I tried logging to a file and the eventviewer. The problem is that the exception is not fired and that's why I can't see anything in the logfile, eventviewer or as I tested before, in a messagebox.

Maybe I have missunderstood you again!

thanks!

--- neteject.com - Internet Solutions ---
 
To make sure your handler is being added correctly, do something like this:
Code:
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf ThreadException
Throw New Exception("foo")
Your handler should be called.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top