views:

176

answers:

4

How to handle the onStop event in the Windows Service created in C#.

A: 

Just have OnStop method overriden:

protected override void OnStop()
{
    // Your code goes here.
}
Andrew Bezzub
A: 

What is it you want to do? If you are fine with your service just stopping, do nothing. You don't need to handle it to actually get the service to stop - it is there to allow you to do stuff when the service receives the stop notification from Windows.

In your class that derives from ServiceBase, you need to override the OnStop method:

protected override void OnStop()
{}

You can then put your logic that should be executed when the service is stopping in there.

Note that Windows allows a short time frame for a service to stop (around 30 seconds I think) - after this it will report that the service cannot stop. This means you cannot do anything too lengthy in the OnStop method. It is usually useful to log that your service received the stop event.

adrianbanks
Well for now i am not doing anything in the onStop method but when i try to stop the service from services.msc it stop fine but then immediately starts again with out me doing anything. Should i be doing something to stop the service when it is stopped manually from services.msc?
Snowill
I have tried writing a try..catch block so as to capture any exception but in vain. I had created a new service, completely fresh without any code,just a sample example but that din't work too.
Snowill
@Snowill- post some sample code please...
RichardOD
Are you sure you are clicking the stop button, not the restart button?
adrianbanks
I definitely am clicking on the stop button and not the restart button.
Snowill
I cannot think of any reason why that would happen unless you have explicitly done it. The only thing I can suggest is that you show a simple example of your code (or possibly a zip of the project somewhere) that demonstrates the issue.
adrianbanks
I have posted a sample of my code as an answer as i dint know how to post it in the comments section. You can go through it.
Snowill
Services can be configured to auto restart upon failure. Check the recovery options on the service properties. Maybe that is what is happening.
Pratik
A: 

Please find below is a code snippet....

protected override void OnStart(string[] args)
            {
                // TODO: Add code here to start your service.

                Timer myTimer = new Timer(5000);
                myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);

                myTimer.Interval = 20000;
                myTimer.Enabled = true;
            }

            private static void myTimer_Elapsed(Object source, ElapsedEventArgs e)
            {
                DBConnection dbc = new DBConnection();
                dbc.sendRequest();
            }

            protected override void OnStop()
            {
                // TODO: Add code here to perform any tear-down necessary to stop your service.
                try
                {
                    logger.Debug("In try block");
                }
                catch (Exception ex)
                {
                    logger.Debug("In catch block : " + ex.Message.ToString());
                }
            }
Snowill
A: 

I suppose this was something to do with the operating system. I deployed my service on Win2k3 and it worked just fine. It started perfectly and stopped when i stopped it from Services.msc. I was trying to deploy the service on WinXP earlier. Please let me know if anyone else has faced such an issue.

Snowill