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 ");
}
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 ");
}