Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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?

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"); 
like image 636
user116969 Avatar asked Mar 28 '13 07:03

user116969


People also ask

How do I run command prompt from command line?

Enter the CLI command. By default, only one command can be executed at one time. Tip: To bulk execute CLI commands, select Batch CLI, and click the icon to select Load CLI Templates. You can also create your own template, or select each device entry in the action panel to add or remove specific commands.

How do I force a run command?

Press Windows+R to open “Run” box. Type “cmd” and then click “OK” to open a regular Command Prompt. Type “cmd” and then press Ctrl+Shift+Enter to open an administrator Command Prompt.

What is an execution command?

EXECUTE forces the data to be read and executes the transformations that precede it in the command sequence.


1 Answers

Use the Process.WaitForExit Method:

 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"); 
like image 90
CloudyMarble Avatar answered Oct 22 '22 06:10

CloudyMarble