System.Diagnostics.Process Information

  • Thread starter Thread starter Ryan Gregg
  • Start date Start date
R

Ryan Gregg

I'm attempting to write a program in .NET that emulates the way the unix
command time works. However, I've run into all sorts of interesting
problems which appear to be related to the way the Process class works. My
code is rather simple, and shows below. However, it seems that once the
process has exited, I am unable to query an of the timing or peak values,
which should seem to be values that would be stored even after the process
finishes executing. Instead, I get exceptions on pretty much any value I
try to query after the process has quit. Is this by design? Why even
bother having these properties if they disappear after the process has
executed! The documentation doesn't indicate anything about an exception
being thrown if you attempt to access these properties after the process has
finished executing.

Thanks in advance.

Ryan Gregg


if (args.Length == 0)
{
Console.WriteLine("Invalid command line arguments");
return;
}

string ExecName, Arguments;
StringBuilder sb = new StringBuilder();

ExecName = args[0];
if (args.Length > 1)
{
for(int i=1; i<args.Length; i++)
sb.Append(" " + args);
Arguments = sb.ToString().Substring(1);
}
else
{
Arguments = String.Empty;
}

Process p = new Process();
p.StartInfo.FileName = ExecName;
p.StartInfo.Arguments = Arguments;
p.StartInfo.UseShellExecute = false;

try
{
p.Start();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return;
}
DateTime StartTime = p.StartTime; // exception occurs here
p.WaitForExit();


if (p.HasExited)
{
// Write out the timing information
DateTime EndTime = p.ExitTime;
TimeSpan runTime = EndTime.Subtract(StartTime);
Console.Write(p.UserProcessorTime.ToString() + "user "); // Exception
here
Console.Write(p.PrivilegedProcessorTime.ToString() + "system "); //
Exception here
Console.WriteLine(runTime.ToString() + "elapsed");
Console.Write(p.PeakWorkingSet.ToString() + "pws "); // exception here
Console.Write(p.PeakPagedMemorySize.ToString() + "ppm "); // exception
here
Console.WriteLine(p.PeakVirtualMemorySize.ToString() + "pvm ");
}
 
Clayton,
Thanks! Sorry it took me so long to notice your reply. I'll give this
a try and see what happens.

Ryan

Clayton Gulick said:
Ryan,

Try enabling the events with EnableRaisingEvents and trap the Exited
event. The properties you are looking for should still be available at that
point.
 
Clay,
I tried out the method you suggested, enabling events and hooking the Exited
event, however I still get the same exception. Any other ideas?

Ryan Gregg
 
Back
Top