I would like to interrupt a command running through cmd.exe. In the code below, I am using ping www.stackoverflow.com -t as an example.
public void Run()
{
System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo("cmd.exe");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.CreateNoWindow = false;
//si.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process console = new Process();
console.StartInfo = si;
console.EnableRaisingEvents = true;
console.OutputDataReceived += proc_OutputDataReceived;
console.ErrorDataReceived += proc_ErrorDataReceived;
console.Start();
console.BeginOutputReadLine();
console.BeginErrorReadLine();
console.StandardInput.WriteLine("ping www.stackoverflow.com -t");
Thread.Sleep(4000);
bool success = GenerateConsoleCtrlEvent((uint)0, (uint)console.SessionId);
if (!success)
{
MessageBox.Show("Error Code: " + Marshal.GetLastWin32Error().ToString());
}
char asdf = (char)0x3;
console.StandardInput.WriteLine('\x3');
console.StandardInput.WriteLine(asdf);
console.StandardInput.Write(asdf);
//console.StandardInput.Close();
console.StandardInput.WriteLine(@"exit");
console.WaitForExit();
}
The error code from GenerateConsoleCtrlEvent is 6.
I have followed the instructions from:
However, I am unable to interrupt the process.
Any help greatly appreciated,