Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Environment.Exit in a windows service

Tags:

c#

exit

service

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.

like image 427
Cygnus Avatar asked Oct 10 '13 10:10

Cygnus


1 Answers

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();
like image 169
Ofiris Avatar answered Nov 06 '22 06:11

Ofiris