I have a console Application. I want it to start a process and end when that process ends. If the user hits ctrl + c, I don't want the application to close. If I set UseShellExecute = false, then this does exactly what I would expect - Ctrl C is ignored.
But it seems that when UseShellExecute:true, the ctrl+c event propagates to the child process and shuts down the child process even if the parent cancels it.
I can't UseShellExecute:true because I want to trap the consoleoutput and consoleerror events.
How can I stop the ctrl+c event from reaching the child process when UseShellExecute:false?
public static int Main(string[] args)
{
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = filename,
Arguments = args,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = false,
}
};
process.OutputDataReceived += process_OutputDataReceived;
process.ErrorDataReceived += process_ErrorDataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
return -1;
}
private static void ConsoleOnCancelKeyPress(object sender, ConsoleCancelEventArgs consoleCancelEventArgs)
{
consoleCancelEventArgs.Cancel = true;
}