1

I have a program that starts a few subprocesses (ProcessStartInfo, Process combo).

The parent application is listening for the cancel key in it's Main function:

Console.CancelKeyPress += (s, ev) =>
{
    // do stuff on Ctrl + C
};

This gets in invoked on the proper key press and all is fine and dandy. The subprocess I start however ALSO are subscribed to Console.CancelKeyPress and they are triggered as well even though when I start them I have their RedirectStandardInput = true set.

So it seems that I have misunderstood something, or that Console.CancelKeyPress is not considered standard input and is instead something special.

I have a specific shutdown sequence I want to employ, and my intention is to shut down the subprocesses when it's appropriate, but because they also receive the Console.CancelKeyPress it makes it impossible to direct them when the right time is to shut down.

Now granted, I could just edit their source and disable that or add some flags to their startup to not subscribe to Console.CancelKeyPress but I am wondering if I am just missing something here as it seems this should not be required, especially I am redirecting the standard input of the subprocess.

I found this somewhat related question: https://stackoverflow.com/a/1802908/1060314

The author states:

My understanding is that when you press CTRL+C in a console, by default all the processes attached to the console receive it, not just the parent one.

AJ Venturella
  • 4,742
  • 4
  • 33
  • 62
  • 1
    Imagine what would happen if it didn't work that way. You'll terminate the program that reads the redirected stdout. But the redirected program would live on and will deadlock since nobody reads its output anymore. Only way to get rid of it is to, say, run Task Manager and kill it. No cue at all that this necessary. That is not a feature that any user ever considers useful. Feature, not a bug. – Hans Passant Jul 28 '18 at 23:20
  • I'm not redirecting stdout, just stdin. I see your point re: stdout, but does that apply to stdin? And would that still apply if nothing was redirected at all? – AJ Venturella Jul 28 '18 at 23:25
  • That doesn't change the failure mode at all. If you don't redirect then it gets to be OS dependent, Windows doesn't mind processes that don't target the console sub-system. – Hans Passant Jul 28 '18 at 23:25

0 Answers0