Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why call base.OnStop() when Windows Service is stopped?

I'm creating a C#.Net Windows Service and am wondering if you always have to call base.OnStop(); in the service's OnStop() method and why?

protected override void OnStop()
{
    threadRunning = false;

    this.ExitCode = 0;
    base.OnStop();
}
like image 486
Petey B Avatar asked Sep 01 '10 16:09

Petey B


1 Answers

From the documentation on ServiceBase.OnStop:

OnStop is expected to be overridden in the derived class. For the service to be useful, OnStart and OnStop should both be implemented in your service class.

So it's really used as a notification to your service that it is stopping.

A glance at the .NET source code for ServiceBase.cs reveals the method does nothing:

protected virtual void OnStop()
{
}

So, do you need to call it? No. But it is still good form to do so. The base implementation may change someday.

like image 137
Paul Williams Avatar answered Oct 21 '22 00:10

Paul Williams