Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Windows service stop itself?

Tags:

I have a Windows service whose startup type is automatic, but I want to do some checks when the service starts, and have the service stop automatically if these checks fail.

How can I do this? My service is written in C#.

like image 775
user1077127 Avatar asked Aug 02 '12 02:08

user1077127


People also ask

Does Windows service run continuously?

Once the win service is started then this process will run continuously in background.

How can I tell if a Windows service has stopped?

Look in the event log: The service control manager logs every time a service is stopped or started. This is Event Viewer -> Windows Logs -> System, where Source is Service Control Manager.

What does stopping a Windows service do?

When a service is paused, it can maintain internal state, including cached information or possibly even a queue of waiting work items. The service can then be resumed to pick up where it left off. If the service is stopped, internal state is discarded.


2 Answers

You can call the Stop method on your ServiceBase class. See msdn for more details.

like image 182
Simon MᶜKenzie Avatar answered Sep 17 '22 15:09

Simon MᶜKenzie


You can use ServiceController and call .stop.

ServiceController sc= new ServiceController(service); sc.Stop(); 
like image 22
Eugene Avatar answered Sep 20 '22 15:09

Eugene