Windows C # Self-Service

How do you end the service?

Environment.Exit will launch the application, but the service will work.

Any idea?

+5
source share
1 answer

You can use SCM to disable a service from the service itself:

System.ServiceProcess.ServiceController svc = new System.ServiceProcess.ServiceController("NameOfYourService");
svc.Stop();

Edit:

Another option is to call ServiceBase.Stop()in your service - if you have access to your instance of the derived class ServiceBase, which will be a cleaner path - in many cases this is cumbersome, although you have to pass this dependency.

, ( , throw), , . , , ServiceBase.Stop().

+14

All Articles