I have a specific situation where I have a process that calls a OS command and I need to stop or kill it. Let's say for example it is a continuous process. The process executed "myapplication.exe" for example. Should I not use the background worker and just wrap it in a thread. Then kill the thread? I also thought about sending a CTRL+C to cancel but not sure how to inject that into a process command. What would be the right path?
private void btnExecuteResponseCmd_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
//https://stackoverflow.com/questions/4732737/how-to-stop-backgroundworker-correctly
//works if it is looping and can read the cancel variable but not in this case
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
ProcessStartInfo pStartInfo = new ProcessStartInfo("myapplication.exe");
//https://social.msdn.microsoft.com/Forums/en-US/5880a108-4169-44a5-81f2-6a745438d486/redirecting-command-window-messages-to-rich-text-box
pStartInfo.CreateNoWindow = true;
pStartInfo.UseShellExecute = false;
pStartInfo.RedirectStandardInput = true;
pStartInfo.RedirectStandardOutput = true;
pStartInfo.RedirectStandardError = true;
process1.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process1.ErrorDataReceived += new DataReceivedEventHandler(ErrorHandler);
process1.StartInfo = pStartInfo;
process1.SynchronizingObject = rbResponse;
process1.Start();
process1.BeginOutputReadLine();
process1.WaitForExit();
}