14

I want to start a program with C# (could use Process.Start()). Then my program should wait until the started program is closed, before it continues.
How do I do this?

marko
  • 9,029
  • 4
  • 30
  • 46
LostPhysx
  • 3,573
  • 8
  • 43
  • 73

2 Answers2

29

After you call Start() add: Process.WaitForExit()

 var myProcess = new Process {StartInfo = new ProcessStartInfo(processPath)};
 myProcess.Start().WaitForExit();
not my real name
  • 393
  • 4
  • 16
Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
  • 3
    You can't pass any arguments to the constructor like you can with the static Start method, however you can supply them using the StartInfo property, e.g. var p = new Process { StartInfo = { FileName = @"cmd.exe", Arguments = ... UseShellExecute = false }, }; p.Start(); p.WaitForExit(); p.Close(); – Rob Sedgwick Jan 06 '14 at 09:50
4

There are two mechanism. You can either hook the Process.Exited event or what you probably really want is to call Process.WaitForExit().

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx

Phillip Scott Givens
  • 5,256
  • 4
  • 32
  • 54