C # Console Calls Batch Batch Files

there, I have two batch files: BatchA.bat , BatchB.bat BatchA.bat calls B1.bat , B2.bat , B3.bat and at the same time runs three batch files (three orders of execution of batch files do not matter). this is BatchA.bat :

 start B1.bat start B2.bat start B3.bat 

BatchB.bat calls B4.bat , B5.bat , B6.bat and simultaneously launches three batch files (three orders of execution of batch files do not matter). this is BatchB.bat :

 start B4.bat start B5.bat start B6.bat 

I use the C # console application to call BatchA.bat , BatchB.bat , but I need to make sure that BatchB.bat will not start until BatchA.bat is complete. In other words, I need to make sure that all B1.bat , B2.bat and B3.bat complete before I start BatchB.bat

This is the C # code:

 proc.StartInfo.FileName = BatchA.bat; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); int exitCode = proc.ExitCode; proc.Close(); Console.WriteLine("Process Complete! exitCode: " + exitCode.ToString()); proc.StartInfo.FileName = BatchB.bat; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); exitCode = proc.ExitCode; proc.Close(); Console.WriteLine("Process Complete! exitCode: " + exitCode.ToString()); 

When I run this, all 6 batch files run at the same time. How to make sure that BatchA.bat not complete until all three small batch files are complete?

+5
source share
5 answers

A simple way to do this is to pass start commands. When the daughter process is completed, the left side of the pipe will be closed, closing the full channel and the batch will continue

So batchA.cmd can be encoded as

 @echo off setlocal enableextensions disabledelayedexpansion echo starting BatchA ( start "b1" cmd /c b1.cmd start "b2" cmd /c b2.cmd start "b3" cmd /c b3.cmd ) | more echo BatchA has ended 

note : this method, although simple, may have a drawback (or not, it depends on the needs). Processes started with b1 ... b3 can support an active channel.

@Squashman has already posted the code so as not to wait for processes to start from internal batch files.

+3
source

This will cause all three batch files to run in their own window in parallel and wait until all three are completed before it starts batchb.

  @echo off start "" /wait cmd /c batch1.bat |start "" /wait cmd /c batch2.bat |start "" /wait cmd /c batch3.bat batchb.bat 
+1
source

You can use WAITFOR . In package A, use this:

 @echo off waitfor done waitfor done waitfor done echo done 

Then in B1, B2, B3 use WAITFOR /SI done when the batch is completed.

Alternatively, you can create a 0-byte marker file in each of B1, B2, and B3 as a monitor, checking for any ones before batch A ends, but is probably more complicated.

+1
source

I see no way to do this with batch files. The "start" command starts the process and does not give you a handle with which you can check whether the process is complete or wait for it to complete.

This can be done using powershell commands.

 PS > $x = Start-Process notepad -PassThru PS > Wait-Process -Id $x.Id 
0
source

Trying to use await and async. Using the await keyword, you can continue other work until BatchA completes.

For instance,

  Task<int> getBatchAData = GetBatchDataAsync(); if (batchAData != 0) { GetBatchB(); } var yourData = await getBatchAData; 

Additional information here: https://msdn.microsoft.com/en-us/library/mt674882.aspx

0
source

Source: https://habr.com/ru/post/1258035/


All Articles