Home > Back-end >  Async C#/XAML how to wait for batch file to complete "await myProcess.StandardInput.WriteLineAs
Async C#/XAML how to wait for batch file to complete "await myProcess.StandardInput.WriteLineAs

Time:10-13

Async C#/XAML/WinUI-3 how to wait for batch file to complete.

In my application I run a long running batch file. The batch file downloads data from the internet that takes 5-10 minutes to download.

The observed behavior of the code below only waits for the WriteLine to complete and then moves along while the batch file keeps running in the background. I want to wait until the batch file completes as downloading the data is the first step and is required before processing the data. Is there a way to accomplish this?

"await myProcess.StandardInput.WriteLineAsync("LongRunning.bat");"

CodePudding user response:

This is how I would do it without knowing more about your specific application and use case. Please note that WaitForExitAsync requires .NET 5 or higher.

var processInfo = new ProcessStartInfo("cmd.exe", "/c LongRunning.bat");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
var process = Process.Start(processInfo);
await process.WaitForExitAsync();
  • Related