Snowill
Snowill

Reputation: 31

Handling onStop event

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

Upvotes: 0

Views: 1579

Answers (2)

adrianbanks
adrianbanks

Reputation: 82994

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.

Upvotes: 1

Andrew Bezzub
Andrew Bezzub

Reputation: 16032

Just have OnStop method overriden:

protected override void OnStop()
{
    // Your code goes here.
}

Upvotes: 0

Related Questions