Search code examples
c#multithreadingwindows-services

Window service OnStop not getting called


I have a C# Windows service. When I try to stop it goes in stopping state and when I placed the break point in onStop method then it is not getting hit. Not able to debug what is problem. I have added exception handling all over the service and also unhandled exception handling but I don't get any error. Every thing is fine with the servie it has canStop set to true and also uses RequestAdditionalTime().

I have also set legacyUnhandledExceptionPolicy enabled="true" in app.config. For me it looks like some threading/resource issue that prevent service control manager to call the OnStop method. Or the service may be in already corrupt state and service control manager can not stop it. It just change the state from Stop to Stopping.

When I try debugging by attaching code to service exe nothing happens when try to stop the service. The onstop method is not getting called.

In Onstart method starts a thread that keep doing a kind of polling on every 30 second. Suppose onStop method gets called then it will abort this thread. This is how this service is developed.

Is there some free tools available to debug if there is some threading issue or my thread in OnStart method is chocking the resource available to service control manager?

Code in OnStop method:

try
{
    RequestAdditionalTime(10*60*1000);
    IntPtr handle = this.ServiceHandle;
    KServiceStatus.currentState = (int) State.SERVICE_START_PENDING;
    SetServiceStatus(handle, ref KServiceStatus);

    RequestAdditionalTime(30000);

    if ((onStartThread == null) ||
         ((onStartThread.ThreadState &
          (ThreadState.Unstarted | ThreadState.Stopped)) != 0))
    {
        onStartThread = new Thread(new ThreadStart(KWindowsServiceHandler.OnStart));
        onStartThread.Start();
    }

    KServiceStatus.currentState = (int) State.SERVICE_RUNNING;
    SetServiceStatus(handle, ref KServiceStatus);
 }              

OnStartThread goes on running until service is running. It required to do a polling. If onStop method is call (which is not being called) will abort this OnStartThread.


Solution

  • Main problem was to find what the problem is. So went on commenting and uncommenting different part of code and found the real issue.OnStartthread launches a process ( which starts one exe) So i kept just this code in the my windows service and removed everything and i was able to reproduce the issue.so what I did is let the OnStartthread sleep for 1s after starting the process. ( the more appropriate would be to start the process and catch the different events to know the status of the process and when it is ready just move) it worked like magic. This is somehow fixes the issue that i was facing.

    When this sleep was not there then process would start but I was not able to hit the break point on OnStop method because the process (exe) got started but it needed some more resource to start fully and be consistent and putting the thread.sleep give the required resource to exe to start fully.

        //the code is in OnStartThread 
    

    someProcess.start(); Thread.Sleep(1000);