Running and waiting for a program to finish

  • Thread starter Thread starter Dylan Parry
  • Start date Start date
D

Dylan Parry

Hi folks,

I'm writing a program that needs to execute an external program and wait
for it to finish running before it can make use of the output from that
program. Specifically, the external program converts from one file
format to another, and is something that I can't do natively :(

So what I need to figure out is how to:

a) Start the external program, with parameters
b) Wait for the external program to finish
c) Do something with the converted file

Now, step c) is the easy part in that I can do that already, but I'm not
sure how to do steps a) and b).

Any help is appreciated, cheers
 
At the simplest level:

using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
using(Process proc = Process.Start("notepad.exe",@"C:\CONFIG.SYS"))
{
proc.WaitForExit();
// can also now look at proc.ExitCode
}
}
}

Marc
 
Hi,

Take a look at the Process class and Process.WaitForExit

Also worth of note is that WaitForExit will block the thread, so if you are
running this in a win app it will freeze. so you better do it in a
background thread.
 
Ignacio said:
Take a look at the Process class and Process.WaitForExit

Yes, thanks, that's the solution recommended by another posted too.
Also worth of note is that WaitForExit will block the thread, so if you are
running this in a win app it will freeze. so you better do it in a
background thread.

Ok, but in my case it won't matter as the program isn't a windows app,
but something running in the background on a server. Thanks for the
warning though - I might have to create a win app at some point that
does something similar.
 
Back
Top