Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide way to send Control+C to proccess #14628

Closed
KindDragon opened this issue May 26, 2015 · 18 comments
Closed

Provide way to send Control+C to proccess #14628

KindDragon opened this issue May 26, 2015 · 18 comments
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Diagnostics.Process help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@KindDragon
Copy link

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/

@khellang
Copy link
Member

Environment.Exit?

@KindDragon
Copy link
Author

Environment.Exit for current process. We must have a way to close created process

@Eyas
Copy link

Eyas commented May 26, 2015

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?

@ghost
Copy link

ghost commented May 26, 2015

How about:

Process.Start("taskkill", "/F /IM [taskname].exe");
// Bonus: kills the entire process tree

Con: not cross-platform.

@jthelin
Copy link

jthelin commented May 26, 2015

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

@ghost
Copy link

ghost commented May 26, 2015

@jthelin is right:

Process.GetProcessById (pid, [machinename])
       .Kill();

@whoisj
Copy link

whoisj commented May 26, 2015

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.

@ghost
Copy link

ghost commented May 26, 2015

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 + C is very difference from killing a process.

👍 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.

@ellismg
Copy link
Contributor

ellismg commented May 28, 2015

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.

@terrajobst
Copy link
Member

Seems related to #14528.

@stephentoub
Copy link
Member

stephentoub commented Sep 29, 2015

Video

This is being tracked by dotnet/corefx#3188.

@stephentoub
Copy link
Member

Sorry, closed prematurely, as the request was for a cross-platform ctrl-C.

@stephentoub stephentoub reopened this Sep 29, 2015
@stephentoub stephentoub removed their assignment Sep 30, 2016
@Priya91
Copy link
Contributor

Priya91 commented Dec 7, 2016

@KindDragon Can you provide an API proposal? A good example here

@wtgodbe
Copy link
Member

wtgodbe commented Nov 30, 2017

Closing as abandoned, feel free to re-open if you plan on working on it.

@wtgodbe wtgodbe closed this as completed Nov 30, 2017
@sebastienros
Copy link
Member

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.

@whoisj
Copy link

whoisj commented Jun 3, 2018

@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' ExitProcess and therefore, their related "at-exit-handlers"; but this never triggers any "signal-handlers".
  • When all else fails, Windows provides TerminateProcess but this is a massive sledgehammer and should be avoided.

@wfurt
Copy link
Member

wfurt commented Jun 4, 2018

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.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@DanielCaspers
Copy link

@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' ExitProcess and therefore, their related "at-exit-handlers"; but this never triggers any "signal-handlers".
  • When all else fails, Windows provides TerminateProcess but 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.

@ghost ghost locked as resolved and limited conversation to collaborators Jan 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Diagnostics.Process help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests