user1077127
user1077127

Reputation: 333

Can a Windows service stop itself?

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#.

Upvotes: 25

Views: 27582

Answers (2)

Simon MᶜKenzie
Simon MᶜKenzie

Reputation: 8664

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

Upvotes: 20

Eugene
Eugene

Reputation: 11535

You can use ServiceController and call .stop.

ServiceController sc= new ServiceController(service);
sc.Stop();

Upvotes: 7

Related Questions