-1

I have created a WPF application with 2 buttons. On the click of first button executing a cmd and waiting for finishing of command because I need to read a result file after execution finish. On the second button click I am stopping execution of cmd by process kill.

But after start command execution, I am not able to stop execution before finishing execution of cmd.

Is there any way of parallel execution?

Start button code

string filename = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "nunit-console.exe");
 Process proc = Process.Start(filename, "/result:" + resultFile + ".xml " + fileName);
proc.WaitForExit();
  • 4
    Use [Multithreading](https://www.tutorialspoint.com/csharp/csharp_multithreading.htm), as your main thread gets freezed while waiting for `proc.WaitForExit();` – boop_the_snoot Aug 17 '17 at 16:58
  • @Nobody is correct. To ellaborate a bit, your UI has its own thread and there needs to be some rules followed to have your UI update from or change other threads. Look into async/await and updating to/from UI Thread. plenty of examples and tutorials out there. – Ginger Ninja Aug 17 '17 at 21:25
  • Note that you may or may not actually need to use `WaitForExit()`. It depends on the rest of the code, which you didn't bother to show. The `Process` class offers asynchronous handling of I/O and state handling. See e.g. https://stackoverflow.com/questions/2085778/c-sharp-read-stdout-of-child-process-asynchronously. Running the above code in a background task or thread is the most obvious approach to a literal reading of your question, but there are other ways to address it. – Peter Duniho Aug 18 '17 at 04:13

1 Answers1

0

Just remove the proc.WaitForExit();. This line is blocking the interface thread, preventing the second button click to be registered.

    System.Diagnostics.Process proc;

    private void Btn1Click(object sender, RoutedEventArgs e)
    {
        string filename = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "nunit-console.exe");
        proc = Process.Start(filename, "/result:" + resultFile + ".xml " + fileName);
    }

    private void Btn2Click(object sender, RoutedEventArgs e)
    {
        if (proc != null)
            proc.Kill();
    }