Home > Software engineering >  Shutdown as if ctrl c was entered
Shutdown as if ctrl c was entered

Time:10-28

I'm trying to shutdown a process a bit more gracefully than what the process.Kill(true), method does.

I have a console application, where I start start another process "myProcess" (which is not an application I have sourcecode for), which I want to be able to shutdown, as if pressing ctrl c in the console. "myProcess" is essentially pinging a receiver at a fixed interval.

When I shut it down using ctrl c the "myProcess" shuts down gracefully, sending an "i am offline now" request to a receiver. However, when I shut it down using kill(true), it doesn't get to send the offline request, and the receiver stops receiving pings, but doesn't mark it as online.

Note I don't have control over the source for myProcess nor the receiver, so I can't change any behaviour in those.

I have tried using waitForExit(), waitForExit(0) and kill(), all of which only stops my debugging session, but myProcess keeps running.

Searching for a solution I come across answers suggestion these methods, and using GenerateConsoleCtrlEvent, to send the ctrl c event programmatically. However my console app does not recognize that method, and I can't seem to find any way to use it. I assume, because the answers were fairly old, that it is no longer an option in .NET 6.

Does anyone know, how to shutdown a process gracefully, wither a spawned chilprocess, or the self process Process.GetCurrentProcess()?

CodePudding user response:

Looks something that needs handling of SIGTERMS.

Check this out

CodePudding user response:

You can try using CloseMainWindow, read the docs to see if it is suitable for your use case.

  • Related