Does calling Environment.Exit from within a Windows service make the SCM call the onStop() method of the service, or is this behaviour unpredictable ?
I have a couple of Windows services that have this code. Unfortunately, I cant remove this behaviour. But I need to know whether onStop() will be called to decide whether to put some common stopping code in that method or within the method that calls Environment.Exit.
Enviroment.Exit
will close the process which is wrapped by the service.
Therefore, calling it, will not call onStop()
method.
You can invoke the method by yourself instead of calling to Exit
You can use the following code to invoke SCM stop
System.ServiceProcess.ServiceController svc = new System.ServiceProcess.ServiceController("NameOfYourService");
svc.Stop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With