Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, June 22, 2014 10:33 PM
Part way through writing a service that starts a process in a separate thread, the process doesn't respond to .Close(), it requires a SIGTERM (15) to perform its shutdown (its a windows binary with unix heritage). Currently I just call .Kill() but this doesn't allow the binary to perform its own clean shutdown. I understand that Windows doesn't use signals, so how do you send a Ctrl-C to a process?
Thanks!
All replies (3)
Monday, June 23, 2014 2:39 AM âś…Answered
This question does not concern C#. GenerateConsoleCtrlEvent is the official Windows API to send a control+C/control+break event to a command line process in the Windows SDK. Problem is that 1 it fails when you don't own a console and 2 it kills the whole process group sharing the console. By default a child process inherit the parent's console, so if you send control+c to a child, the parent process could be killed too, possibly with other child processes.
Since you gotta to create a console to call GenerateConsoleCtrlEvent, and you can only have one console per process, better do it in a child console process to isolate the effect on your service process (multiple services could be running in the same process).
Note a console process may not honor a termination signal. You still need to kill the process as the last resort.
If you need help in calling these Windows APIs in C#, you can look up in www.pinvoke.net, or ask in the CLR forum.
Sunday, June 22, 2014 11:02 PM
You call GenerateConsoleCtrlEvent with CTRL_C_EVENT, but then you need to write another console process so you can invoke it in the same console group with your target process.
Monday, June 23, 2014 1:15 AM
Hi Sheng,
Would you mind elaborating a bit as I am extremely new to C# and not very clear how to accomplish this.
Thanks!