We help IT Professionals succeed at work.

C# Window service OnStop not getting called

courion
courion used Ask the Experts™
on
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. But of no help.
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.
Can someone please advice me on how to debug this issue?
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?
Kindly help me it's urgent.

Thanks,
Courion
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi,
    A couple of quick questions:

Did you override the OnStop method with this declaration: protected override void OnStop()?
Are there any events in the Windows Event logs to indicate any reasons for errors?

     Just the fact that it shows in the SCM leads me to believe the installer works and the service is installed correctly - however you may want to make sure of that. Another thing to check real quick is in Visual Studio (VS), right click your .cs file that is the service, select View Designer, then look at the properties to make sure the CanStop property is set to True.
Commented:
You say you have a thread polling in every 30 seconds... is it using a Wait(), Join(), Sleep(), or some other method to pause for the 30 seconds?  How are you creating the thread?  And how are you stopping that poll thread when your OnStop gets called?

Some code snippets of your core service class would be helpful.