Here is a C# example where one can execute a program using the Process Class. The example launches notepad with an argument of a filename to open and waits for completion. When the user closes notepad, its return code is printed out.
Console.WriteLine("Running");
Process pr = new Process();
pr.StartInfo.FileName = "Notepad.exe";
pr.StartInfo.Arguments = "test.dat";
pr.Start();
while (pr.HasExited == false)
if ((DateTime.Now.Second % 5) == 0)
{ // Show a tick every five seconds.
Console.Write(".");
System.Threading.Thread.Sleep(1000);
}
One can extract any output that a program does to the command line by looking at the Process.StandardOutput Property if it is applicable.
#1 by Eize on October 17, 2008 - 5:32 am
Quote
Ouch this will create a busy loop for 4 seconds every time ..
The 1 second sleep is only done once every 5 seconds ..
You want to move the sleep outside the ‘if’
while (pr.HasExited == false)
{
if ((DateTime.Now.Second % 5) == 0)
{ // Show a tick every five seconds.
Console.Write(“.”);
}
System.Threading.Thread.Sleep(1000);
}
#2 by omegaman on October 17, 2008 - 10:00 am
Quote
You are right! In my defense, this article was meant for demonstration purposes and was not optimized.
#3 by Dotnetmania on February 17, 2009 - 8:50 am
Quote
Great knowledge! Thanks for sharing this, it’s saved my time.
#4 by Aksu on April 13, 2009 - 4:57 am
Quote
Nice snippet! Including “using” statements would also be very helpful
#5 by Hani on February 19, 2010 - 2:22 pm
Quote
It might be useful to know that Process is contained in System.Diagnostics