0

Hello in my main form I am running a command-line exe to do some stuff for me. The only problem is as it is doing the stuff it needs to do my parent form is completely frozen until the process has ended.

Is there any way to run the process while keeping my parent form not frozen so buttons can be clicked, etc? Thank you, I have provided my code below for running the process.

        Process newprocess = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.FileName = @"C:\tool.exe";
        startInfo.Arguments = "Some Arguments";
        newprocess.StartInfo = startInfo;
        newprocess.Start();

        newprocess.WaitForExit();
SockNastre
  • 15
  • 1
  • 4

1 Answers1

1

Put the code in your question into a method, called say RunProcessAndWait(), and then in your event handler call Task.Run(() => RunProcessAndWait());

If you need to perform UI actions in the event, after the process has completed, use async:

private async void OnSomethingPressed(...) 
{
    await Task.Run(() => RunProcessAndWait());
    // Do UI (or other) things here
}

If you'd like to make the waiting more truly async - probably not necessary here, but for completeness, and it will save you one thread for a while - the SO question referred to in the comments has an answer showing how to do this.

sellotape
  • 8,034
  • 2
  • 26
  • 30