locked
Catching exceptions in a windows service RRS feed

  • Question

  • Hi

    I have configured crash handling for my app so it create a log containing information I need to debug and it works fine in a winforms app

    In one windows service it also worked fine, but then when trying it in another it wasn't firing

    It seems to be to do with what thread my code is running on

    In my OnStart method I have 

      AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomain_UnhandledException

    And then I have the code in my handler which calls my class library and deals with showing or writing to event log or to file etc - this bit is not so relevant to the issue

      Private Sub AppDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
            MyCrashHandler.Save(DirectCast(e.ExceptionObject, Exception), handled:=False)
       End Sub

    Now in one service where the exception occurs in a  "Public Async Sub" it fires the handler perfectly fine

    But in another service where it is occurring in a "Public Sub" it doesn't fire

    I am assuming the Async and being on a different thread is the clue to why here

    So my question is how can I handle exceptions which happen even if running in same thread?? or am I missing something

    I have lots of services I have deployed which work perfectly well without Async or any threading, it is only now I decide to add exception logging to them that I have the issue

    Thanks


    Darren Rose

    Friday, February 22, 2019 11:04 PM

All replies

  • upon further research I think it might be how I have written my services

    Lots of articles mention putting as little code as possible in the OnStart method and then calling a sub in a new thread to do the work - if I did this then being in a new thread I suspect the exception handling would work

    Thoughts/comments please


    Darren Rose

    Saturday, February 23, 2019 1:21 AM
  • I've only written services in C# yet C# or VB.NET little as possible goes into OnStart.

    Example

    protected override void OnStart(string[] args)
    {
        RequestAdditionalTime(10000);
        ScheduleService();
    }

    From there I write to the event log when there is a chance of an exception. For debugging I have a starter utility which will work for VB.NET or C# found at the following repository on GitHub.

    https://github.com/karenpayneoregon/WindowsInstaller


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 23, 2019 2:38 AM
  • I would say spawn a new thread in the OnStart() and on the initialization of the new thread but before the While loop to keep the thread active do the Addhandler () for the GEH. 
    Saturday, February 23, 2019 5:59 AM
  • Thanks both

    Yes from my research I was thinking that too, whereas currently I just call another sub from OnStart so therefore in most case it runs in same thread - which is the issue I believe

    What is best way of calling another thread in a service? seen couple of different examples as below, but not sure if one better than other etc

    e.g.

    Public Shared Sub Start()
    	Dim currentDomain As AppDomain = AppDomain.CurrentDomain
    	AddHandler currentDomain.UnhandledException, AddressOf currentDomain_UnhandledException
    
    	running = True
    	Dim ts As New ThreadStart(AddressOf ServiceThreadBody)
    	thread = New Thread(ts)
    	thread.Name = "ServiceThread"
    	thread.Priority = ThreadPriority.BelowNormal
    	thread.Start()
    End Sub

    or

    Protected Overrides Sub OnStart(ByVal args() As String) 
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ServiceWorkerThread)) 
        End Sub 

    If doing it like this do I have to do anything different re OnStop, e.g. regularly checking for stop event to call OnStop, or does it just work as it does now without a separate thread


    Darren Rose

    Saturday, February 23, 2019 11:51 AM