-1

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();
}
Zippy
  • 455
  • 1
  • 11
  • 25
  • 2
    Why do you execute ping in a cmd instead of ping.exe directly? Why aren't you using [System.Net.NetworkInformation.Ping](https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ping?view=netcore-3.1) ? – Fildor Sep 08 '20 at 12:03
  • this was an example of a long running command. swap the cmd ping with my command that does the same thing – Zippy Sep 08 '20 at 12:09
  • Ok, so you are not _actually_ using ping... alright. Now, what's blocking here, is the call to WaitForExit. You _could_ do a spin-wait and check cancel. Not beautiful but perhaps you can go step by step, once you have something that works at all. – Fildor Sep 08 '20 at 12:13
  • 1
    Why not use process1.Kill() whenever you want to kill the process instead of trying to send CTRL+C – gkulshrestha Sep 08 '20 at 12:15
  • 1
    Instead of `process1.WaitForExit()` implement your own waiting logic or check for the output in your `OutputDataReceived` handler and based on that stop/kill the process. It's not completely clear what you're trying to do. You can just use `process1.WaitForExit(timeInMs)` as well. – Konrad Sep 08 '20 at 12:15
  • 1
    ^^ If you detect cancel, you can of course send signals / kill it. – Fildor Sep 08 '20 at 12:16
  • 1
    A completely different approach would be of course "does it _really need to be_ a cmd?" – Fildor Sep 08 '20 at 12:19
  • Ok, good suggestions. My command is a external .exe that runs without waiting for a command. It does stuff so not worth explaining here to solve the problem. Just that it runs without waiting for input. I will try some of the suggestions and see what I get going. Thanks – Zippy Sep 08 '20 at 12:28

1 Answers1

0

Ok, I found how to control the cancellation and looks like it kills the process just fine. I will have to explore if it releases all resources, and this is the best way to do it.

private void btnStopCommand_Click(object sender, EventArgs e)
{
    process1.CancelOutputRead();
    process1.Kill();
}
Zippy
  • 455
  • 1
  • 11
  • 25