[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

How do you exit a Windows Service programmatically from within the service?

I am creating a windows service application. It has a configuration file that is checked at startup.  If the configuration file isn't correct, I want to stop the service.  What is the best way to do this?  A C# code example would be very useful.
1 Solution
You should be able to use the ServiceController object to do this, from inside the Service class:

      ServiceController sc = new ServiceController(this.ServiceName);

I haven't actually tested this, but it should work. Let me know how it goes, and I'll look into it deeper if you have problems with it.
rfischernw7Author Commented:
I will give it a try

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now