Currently .NET doesn't have way to exit nicely from created console process.
It would be nice if we had a such a method.
namespace System.Diagnostics
{
public class Process
{
public void SendCtrlCSignal()
{
...
}
}
}
It can be implemented following way on Windows: http://stackoverflow.com/a/15281070/61505 , for unix we can call kill (pid, SIGINT); http://stackoverflow.com/a/1761182/61505
It provide a lot of problems for other users too http://stanislavs.org/stopping-command-line-applications-programatically-with-ctrl-c-events-from-net/
Environment.Exit for current process. We must have a way to close created process
My understanding is that Windows is inherently different from Unix here. The best way to provide "signaling" abilities to a Windows process is to launch it as a service, as far as I know.
Is there a non-hacky way (in any language/API) for one Windows process to kill another?
How about:
C#
Process.Start("taskkill", "/F /IM [taskname].exe");
// Bonus: kills the entire process tree
Con: not cross-platform.
Is there some specific reason why Process.Kill or Process.CloseMainWindow won't work for this scenario?
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow.aspx
@jthelin is right:
c#
Process.GetProcessById (pid, [machinename])
.Kill();
Sending Ctrl + C is very difference from killing a process. Just because they happen to be the same by convention does not mean they are the same. Seems like the title of this topic is a misnomer.
The functionality sought by the creator of this topic is provided by @jthelin. A proper way to send control characters to console processes via C#? Write to their standard in pipe.
Write to their standard in pipe.
Only if the process is reading from stdin. See this and the comment below: http://stackoverflow.com/a/285041
Sending
Ctrl + Cis very difference from killing a process.
:+1: for providing OOTB signaling, which works cross-platform.
This http://stackoverflow.com/a/7323673 (and the other answer by Vitaliy Fedorchenko) has some good pointers.
Mono has its own implementation for Unix signaling: http://www.jprl.com/Blog/archive/development/mono/2008/Feb-08.html.
FWIW, I worry very much about implementing something like this. The semantic between how this would work on *NIX (where you can actually send an event to a single process) vs Windows, where it goes to possibly multiple process, is worry-some to me. I would much prefer some library outside of the core to handle this.
I am glad we also did not expose Process.CloseMainWindow() as part of .NET Core for similar reasons, it would be very hard to support across multiple OSes and App Models.
Seems related to dotnet/runtime#14528.
This is being tracked by dotnet/corefx#3188.
Sorry, closed prematurely, as the request was for a cross-platform ctrl-C.
@KindDragon Can you provide an API proposal? A good example here
Closing as abandoned, feel free to re-open if you plan on working on it.
To add my input, I am starting processing from a web application (benchmarking them) and can't get anything to work correctly in either windows or linux. P/Invoke, kill -INT, nothing works and it's really an issue for me. The only workaround I got so far is to have an endpoint in these web apps to trigger an internal graceful shutdown, let's call it a backdoor. But I can do this for shell scripts.
@ellismg @terrajobst @stephentoub we (VS Version Control & Git for Windows) have had to deal with this issue re: Git-bash. We've built up a scary level of knowledge about how Ctrl+C "signals" work on Windows. If / when you need information, feel free to reach out and I can provide a brain dump plus sounding board.
Sadly, I've negative spare time to contribute outside my core area at the moment.
Quick overview: POSIX API enable sending signals to any process. Windows enables sending signals to any process you own, and have access to their attached console. Windows makes this extra complicated by:
ExitProcess and therefore, their related "at-exit-handlers"; but this never triggers any "signal-handlers".TerminateProcess but this is a massive sledgehammer and should be avoided.At least on Unix, the signals can be handled or ignored by application @sebastienros.
I don't know if that may be your problem but background tasks may do some handling there.
You can also try SIGTERM (15) -> ask process to terminate.
You may be to use Mono.Posix but as discussed above, Windows do not really have POSIX signals.
@ellismg @terrajobst @stephentoub we (VS Version Control & Git for Windows) have had to deal with this issue re: Git-bash. We've built up a scary level of knowledge about how Ctrl+C "signals" work on Windows. If / when you need information, feel free to reach out and I can provide a brain dump plus sounding board.
Sadly, I've negative spare time to contribute outside my core area at the moment.
Quick overview: POSIX API enable sending signals to any process. Windows enables sending signals to any process you own, and have access to their attached console. Windows makes this extra complicated by:
- Windows does all "signal" management via conhost.exe; this is because Windows is an API based operating system, whereas POISIX operating systems are more file-handle aligned.
- Processes started via ShellExecute are effectively immune to signal handling processing.
- Windows provides API to trigger another process'
ExitProcessand therefore, their related "at-exit-handlers"; but this never triggers any "signal-handlers".- When all else fails, Windows provides
TerminateProcessbut this is a massive sledgehammer and should be avoided.
@whoisj , I would love to take you up on your offer to tap into your knowledge on Ctrl+C "signals" and that brain dump...
I am running up against a similar issue to what @sebastienros mentioned, except I am looking to gracefully stop a .NET Core 3.1 process from NodeJS 12+ on Windows. I've been all over through the depths of NodeJS (libuv in C++) and dotnet/runtime trying to find an approach, and am more or less stumped.
I am interested in the ability to trigger another process' ExitProcess so that the .NET Core 3.1 app can receive the "signal" in ConsoleLifetime handler which is bound to AppDomain.CurrentDomain.ProcessExit so that its handlers in my app can be called. If you could share what Win32 APIs I should be directing my attention towards, I'd be very grateful.
Most helpful comment
Sending
Ctrl + Cis very difference from killing a process. Just because they happen to be the same by convention does not mean they are the same. Seems like the title of this topic is a misnomer.The functionality sought by the creator of this topic is provided by @jthelin. A proper way to send control characters to console processes via C#? Write to their standard in pipe.