title: Window service OnStop not getting called
body: <p>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().</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Code in OnStop method:</p>
  
  <pre><code>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 &amp;
            (ThreadState.Unstarted | ThreadState.Stopped)) != 0))
      {
          onStartThread = new Thread(new ThreadStart(KWindowsServiceHandler.OnStart));
          onStartThread.Start();
      }
  
      KServiceStatus.currentState = (int) State.SERVICE_RUNNING;
      SetServiceStatus(handle, ref KServiceStatus);
   }              
  </code></pre>
  
  <p>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.</p>