Pass cmd argument to running process when shutting down windows service

196 views Asked by At

I am creating a windowservice that starts a process where I pass multiple cmd arguments in the StartInfo.Arguments. That works fine as expected.

The problem is that when i stop the windowsservice, I need to tell the running program to shut down gracefully before the windows service stops. Because if I just kill the process the program does not save the collected data. The program that I run is the Process Monitor and it accepts the following command "/terminate".

How can I pass the /terminate argument before my windows service stops?

2

There are 2 answers

0
mnemonic On

There is a couple of ways you can approach this solution but you can signal the process, and based on what you send with this signal it should execute its 'clean-up' code. Once it's finished, the process that spawned it should be signalled that it finished the cleanup code and can then shutdown. This of course assumes that you wrote the code for both the Window service and the process being spawned.

You can try Named pipes (https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication)

0
IceCode On

I managed to find a solution. In the OnStop() method in the service, I needed to create a new instance of the Process Monitor and pass the /Terminate argument:

Process process = new Process();
process.FileName = "\Procmon.exe" ;
process.StartInfo.Arguments = "/AcceptEula /Terminate";
process.Start();

With that it stoppes all the Process Monitor instances gracefully.