Mobile Apps and Web Solutions

Starting off a new process

Originally posted on my old MSDN blog

Reasonably often I find myself writing a mini-test harness when I want to run a console application and time how long it takes.

I always end up looking back through old code to remember how to do it, so this makes it very suitable for posting here. It's not particularly revealing or complicated code, but hopefully it may save you a few minutes if you want to do the same thing.

Here's the code:

Stopwatch timer = new Stopwatch();
Process process = new Process();

// Set up the process information
ProcessStartInfo info = new ProcessStartInfo();
info.UseShellExecute = true;
info.CreateNoWindow = false;
info.WindowStyle = ProcessWindowStyle.Minimized;
info.FileName = "YourConsoleApp.exe";
info.RedirectStandardOutput = false;
process.StartInfo = info;

// Start the timer, and then the process
timer.Start();
process.Start();
process.WaitForExit();
timer.Stop();

Console.WriteLine("Running application took {0} seconds", timer.ElapsedMilliseconds / 1000);
N.B. This blog post has now moved to https://writingontablets.com