How to wait for my batch file to finish

I am making a program where I need to run cmd and run a batch file. The problem is what I am using MyProcess.WaithForexit();

and I think it is not waiting for the batch file to finish processing. It just waits until cmd is closed. My code:

System.Diagnostics.ProcessStartInfo ProcStartInfo =
    new System.Diagnostics.ProcessStartInfo("cmd");
    ProcStartInfo.RedirectStandardOutput = true;
    ProcStartInfo.UseShellExecute = false;
    ProcStartInfo.CreateNoWindow = false;
    ProcStartInfo.RedirectStandardError = true;
    System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
    ProcStartInfo.Arguments = "/c start batch.bat ";
    MyProcess.StartInfo = ProcStartInfo;
    MyProcess.Start();
    MyProcess.WaitForExit();

      

I need to wait for the batch file to finish. How should I do it?

+3


source to share


1 answer


The run command has arguments that can make it WAIT

to a terminated program. Modify the arguments shown below to pass '/ wait':

ProcStartInfo.Arguments = "/c start /wait batch.bat ";

      

I would also assume that you want your batch file to exit from the cmd environment, so put "exit" at the end of the package.



@echo off
rem Do processing
exit

      

This should provide the desired behavior.

+2


source







All Articles