Call environment. Logout to Windows Service

Does calling Environment.Exit from a Windows service cause SCM to call the onStop () method of the service, or is this behavior unpredictable?

I have several Windows services that have this code. Sorry, I cannot remove this behavior. But I need to know if onStop () will be called to decide whether to put some common stop code in this method or inside the method that calls Environment.Exit.

+8
c # service exit
source share
1 answer

Enviroment.Exit will close the process that is wrapped by the service.

Therefore, calling it will not call the onStop() method.

You can call the method yourself, instead of calling Exit

You can use the following code to call SCM stop

 System.ServiceProcess.ServiceController svc = new System.ServiceProcess.ServiceController("NameOfYourService"); svc.Stop(); 
+11
source share

All Articles