如何要等到我的批处理文件完成

如何要等到我的批处理文件完成

本文介绍了如何要等到我的批处理文件完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个程序,我需要开始cmd并有启动的批处理文件。问题是,我使用 MyProcess.WaithForexit(); ,我认为它不会等到该批处理文件处理完成。它只是等待,直到CMD关闭。我的code迄今:

I'm doing a program where I need to start cmd and there start up a batch file. The problem is that I'm using MyProcess.WaithForexit(); and I think it does not wait until the batch file processing is finished. It just waits until the cmd is closed. My code so far:

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 until the batch file is finished. How do I do that?

推荐答案

start命令有参数,可以让 WAIT 的启动程序来完成。编辑参数,如下所示通过'/等待:

The start command has arguments that can make it WAIT for the started program to complete. Edit the arguments as show below to pass '/wait':

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

我也建议您要您的批处理文件,退出CMD envirionment所以放置一个退出在批处理结束。

I would also suggest that you want your batch file to exit the cmd envirionment so place an 'exit' at the end of the batch.

@echo off
rem Do processing
exit

这应达到所期望的行为。

This should achieve the desired behavior.

这篇关于如何要等到我的批处理文件完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:13