C# 如何通过命令行执行命令并等待它完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15676058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:32:47  来源:igfitidea点击:

How to execute a command via command-line and wait for it to be done

c#

提问by user116969

I'm trying to execute a command via command-line and afterwards execute another command (not in cmd) which dependes on the outcome of the former command. The problem is that the first command takes about 2 minutes to end, and the 2nd command won't "wait" for the first one to end. How can I hold the 2nd command to wait until the first ends?

我正在尝试通过命令行执行命令,然后执行另一个命令(不在 cmd 中),这取决于前一个命令的结果。问题是第一个命令需要大约 2 分钟才能结束,第二个命令不会“等待”第一个命令结束。我怎样才能保持第二个命令等到第一个结束?

Thanks in advance!

提前致谢!

public void runCmd(){
  String command = @"/k java -jar myJava.jar";
  ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
  cmdsi.Arguments = command;
  Process cmd = Process.Start(cmdsi);
}
.
.
.
runCmd();           //first command, takes 2 minutes to finish
MessageBox.Show("This Should popup only when runCmd() finishes");

采纳答案by CloudyMarble

Use the Process.WaitForExitMethod:

使用Process.WaitForExit方法:

 public void runCmd()
 {
    String command = @"/k java -jar myJava.jar";
    ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
    cmdsi.Arguments = command;
    Process cmd = Process.Start(cmdsi);
    cmd.WaitForExit();    
 }
.
.
.
 runCmd(); ? ? ? ?
 MessageBox.Show("This Should popup only when runCmd() finishes");

回答by jacob aloysious

You could use WaitForExit().

您可以使用 WaitForExit()。

Note:

注意

  1. WaitForExit(int milliseconds)to wait the specified number of milliseconds for the associated process to exit.
  2. WaitForExit()wait indefinitely for the associated process to exit.
  1. WaitForExit(int milliseconds)等待指定的毫秒数让关联进程退出。
  2. WaitForExit()无限期等待相关进程退出。
public void runCmd()
{
  String command = @"/k java -jar myJava.jar";
  ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
  cmdsi.Arguments = command;
  Process cmd = Process.Start(cmdsi);
  cmd.WaitForExit(); //wait indefinitely for the associated process to exit.
}
.
.
.
runCmd();           //first command, takes 2 minutes to finish
MessageBox.Show("This Should popup only when runCmd() finishes");
public void runCmd()
{
  String command = @"/k java -jar myJava.jar";
  ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
  cmdsi.Arguments = command;
  Process cmd = Process.Start(cmdsi);
  cmd.WaitForExit(); //wait indefinitely for the associated process to exit.
}
.
.
.
runCmd();           //first command, takes 2 minutes to finish
MessageBox.Show("This Should popup only when runCmd() finishes");

回答by Soner G?nül

You can use Process.WaitForExit()method;

您可以使用Process.WaitForExit()方法;

Instructs the Process component to wait indefinitely for the associated process to exit.

指示 Process 组件无限期地等待关联的进程退出。

// Start the process with the info you specified.
// Call WaitForExit and then the using statement will close.
using(Process cmd = Process.Start(cmdsi))
{
      cmd.WaitForExit();
}

回答by Sean

Using the WaitForExit();process would be best as others have stated.

WaitForExit();正如其他人所说,最好使用该过程。

specifying a time in milliseconds for the command to exit or indefinitely as advised.

按照建议指定命令退出或无限期的时间(以毫秒为单位)。

回答by Fittizio Casoncelli

Even shorter:

更短:

Process.Start("cmd.exe", @"/k java -jar myJava.jar").WaitForExit();

This works because the static method Process.Startreturns a Processobject. Then you can call the WaitForExitmethod directly on it, without even storing it in a local variable.

这是有效的,因为静态方法Process.Start返回一个Process对象。然后你可以WaitForExit直接在它上面调用这个方法,甚至不需要把它存储在一个局部变量中。