views:

52

answers:

3
 Log("Starting to compile...\n");
        Process p = new Process();
        p.StartInfo.FileName = CompilationCommand;
        p.StartInfo.Arguments = SourceFileName;
        p.Start();
        Log("Compiling finished in " + (p.StartTime - p.ExitTime).Seconds + " seconds\n");

This code doesn't work. How can I find out how much time was spent on compiling a C++ source file with g++ compiler?

+1  A: 

Processes run asynchronously to other processes but you don't wait for it to finish. By the time your Log call returns, the process could still be running away.

DeadMG
+2  A: 

You can use the WaitForExit function in process to wait for the process to exit along with the Stopwatch class to measure the time it took. This would result in you having a thread waiting for the process to exit.

Stopwatch watch = Stopwatch.StartNew();
// start process as before
process.WaitForExit();
TimeSpan ts = watch.Elapsed;
Patrick
Thanks! That's exactly what I was looking for.
Alex
+1  A: 

Have a look at implementing the process.Exited event.

This MSDN article has a good sample:

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

Russ C