Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 37121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:30:03+00:00 2026-05-10T14:30:03+00:00

Is it possible to use an UnhandledException Handler in a Windows Service? Normally I

  • 0

Is it possible to use an UnhandledException Handler in a Windows Service?

Normally I would use a custom built Exception Handling Component that does logging, phone home, etc. This component adds a handler to System.AppDomain.CurrentDomain.UnhandledException but as far as I can tell this doesn’t achieve anything win a Windows Service so I end up with this pattern in my 2 (or 4) Service entry points:

      Protected Overrides Sub OnStart(ByVal args() As String)         ' Add code here to start your service. This method should set things         ' in motion so your service can do its work.         Try             MyServiceComponent.Start()         Catch ex As Exception             'call into our exception handler             MyExceptionHandlingComponent.ManuallyHandleException (ex)             'zero is the default ExitCode for a successfull exit, so if we set it to non-zero             ExitCode = -1             'So, we use Environment.Exit, it seems to be the most appropriate thing to use             'we pass an exit code here as well, just in case.             System.Environment.Exit(-1)         End Try     End Sub  

Is there a way my Custom Exception Handling component can deal with this better so I don’t have to fill my OnStart with messy exception handling plumbing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:30:04+00:00Added an answer on May 10, 2026 at 2:30 pm

    Ok, I’ve done a little more research into this now. When you create a windows service in .Net, you create a class that inherits from System.ServiceProcess.ServiceBase (In VB this is hidden in the .Designer.vb file). You then override the OnStart and OnStop function, and OnPause and OnContinue if you choose to. These methods are invoked from within the base class so I did a little poking around with reflector. OnStart is invoked by a method in System.ServiceProcess.ServiceBase called ServiceQueuedMainCallback. The vesion on my machine ‘System.ServiceProcess, Version=2.0.0.0’ decompiles like this:

      Private Sub ServiceQueuedMainCallback(ByVal state As Object)     Dim args As String() = DirectCast(state, String())     Try          Me.OnStart(args)         Me.WriteEventLogEntry(Res.GetString('StartSuccessful'))         Me.status.checkPoint = 0         Me.status.waitHint = 0         Me.status.currentState = 4     Catch exception As Exception         Me.WriteEventLogEntry(Res.GetString('StartFailed', New Object() { exception.ToString }), EventLogEntryType.Error)         Me.status.currentState = 1     Catch obj1 As Object         Me.WriteEventLogEntry(Res.GetString('StartFailed', New Object() { String.Empty }), EventLogEntryType.Error)         Me.status.currentState = 1     End Try     Me.startCompletedSignal.Set End Sub  

    So because Me.OnStart(args) is called from within the Try portion of a Try Catch block I assume that anything that happens within the OnStart method is effectively wrapped by that Try Catch block and therefore any exceptions that occur aren’t technically unhandled as they are actually handled in the ServiceQueuedMainCallback Try Catch. So CurrentDomain.UnhandledException never actually happens at least during the startup routine. The other 3 entry points (OnStop, OnPause and OnContinue) are all called from the base class in a similar way.

    So I ‘think’ that explains why my Exception Handling component can’t catch UnhandledException on Start and Stop, but I’m not sure if it explains why timers that are setup in OnStart can’t cause an UnhandledException when they fire.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could use Reflections, which is a Java runtime metadata… May 11, 2026 at 12:16 pm
  • added an answer A TableLayoutPanel is probably your best choice, as the row… May 11, 2026 at 12:16 pm
  • added an answer Consider using BindingList<T> or ObservableCollection<T>; both of these do what… May 11, 2026 at 12:16 pm

Related Questions

Is it possible to use an UnhandledException Handler in a Windows Service? Normally I
Is it possible to use an IF clause within a WHERE clause in MS
Is it possible to use an ocx (ActiveX Control) on a winform (probably adding
Is it possible to use an image captured with the iPhone's camera as a
Is it possible to use JavaScript to open an HTML select to show its
Is it possible to use overlapped I/O with an anonymous pipe? CreatePipe() does not
Is it possible to use PHP's SimpleXML functions to create an XML object from
Is it possible to use single XML file for Ruby on Rails as an
Is it possible to use e.g. SQLite with PowerBuilder? I need an embedded open
Is it possible to use a flash document embedded in HTML as a link?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.