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
#6 by hunter2 on June 26, 2012 - 11:47 am
Quote
wow, you just poorly copied the code from a C# tutorial here http://www.csharp-station.com/HowTo/ProcessStart.aspx
I wish I could be as cool as you
#7 by OmegaMan on June 28, 2012 - 7:48 am
Quote
Two things
1) Yes Joe Mayo’s post is much more detailed and the commonality besides the process is the use of Notepad to launch. While his was meant as a detailed learning experience written in the heady days of .Net 1 my article was written as a quick lookup example for MSDN forum posts years later. Notepad was used because everyone has notepad and launching that seems; well obvious. I didn’t even know Joe’s article existed until now actually and frankly except for the topic and use of Notepad, there is very little similarity (yes his is better).
2) I am actually friends with Joe Mayo. He lives here in Denver and we both helpout/speak for the Denver Visual Studio’s User Group and as mentioned do I consider him a friend. Don’t believe me? Check out Joe’s tweet when he was going to see me speak on Silverlight 5:
So to sum up, I did not plagiarize Joe Mayo’s work; nor never have. Similarities on thier own do not lend themselves to a proof. My blog regularly gets plagiarized verbatim as I am sure Joe’s writings do too and that is a problem on the net…. But I do not need to copy someone else’s work; I may leverage it in writing a new article, but since my days as an editor on my school paper (I was thinking about a journalism major at one point before deciding on CS) I take plagiarism and libel very seriously and would never engage in that activity.
PS: As to being cool, well cool is as cool does….
#8 by Joe Mayo on June 28, 2012 - 8:45 am
Quote
I’m sure any similarity is coincidental – this is fine. Thanks for the concern.
Joe