看看这个例子
using System;
using System.Diagnostics;
using System.Threading;namespace Proecess_Sample
{
class MyProcessClass
{
public static void Main()
{
try
{ Process myProcess;
myProcess = Process.Start("NotePad.exe");
while(true)
{
// Get associated process's physical memory usage.
Console.WriteLine("Process's physical memory usage:" + myProcess.WorkingSet);
// Get base priority of the associated process.
Console.WriteLine("Base priority of the associated process:" + myProcess.BasePriority);
// Get user processor time for this process.
Console.WriteLine("User Processor Time:" + myProcess.UserProcessorTime);
// Get privileged processor time for this process.
Console.WriteLine("Privileged ProcessorTime:" + myProcess.PrivilegedProcessorTime);
// Get total processor time for this process.
Console.WriteLine("Total Processor Time:" + myProcess.TotalProcessorTime);
// Invoke overloaded ToString function.
Console.WriteLine("Process's Name:" + myProcess.ToString());
Console.WriteLine("-------------------------------------");
if(myProcess.HasExited)
{
Console.WriteLine("The process has exited");
break;
}
if(myProcess.Responding)
{
Console.WriteLine("Status:Responding to user interface");
myProcess.Refresh();
}
else
Console.WriteLine("Status:Not Responding");
Thread.Sleep(1000); }
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised" + e.Message);
}
} }
}
using System;
using System.Diagnostics;
using System.Threading;namespace Proecess_Sample
{
class MyProcessClass
{
public static void Main()
{
try
{ Process myProcess;
myProcess = Process.Start("NotePad.exe");
while(true)
{
// Get associated process's physical memory usage.
Console.WriteLine("Process's physical memory usage:" + myProcess.WorkingSet);
// Get base priority of the associated process.
Console.WriteLine("Base priority of the associated process:" + myProcess.BasePriority);
// Get user processor time for this process.
Console.WriteLine("User Processor Time:" + myProcess.UserProcessorTime);
// Get privileged processor time for this process.
Console.WriteLine("Privileged ProcessorTime:" + myProcess.PrivilegedProcessorTime);
// Get total processor time for this process.
Console.WriteLine("Total Processor Time:" + myProcess.TotalProcessorTime);
// Invoke overloaded ToString function.
Console.WriteLine("Process's Name:" + myProcess.ToString());
Console.WriteLine("-------------------------------------");
if(myProcess.HasExited)
{
Console.WriteLine("The process has exited");
break;
}
if(myProcess.Responding)
{
Console.WriteLine("Status:Responding to user interface");
myProcess.Refresh();
}
else
Console.WriteLine("Status:Not Responding");
Thread.Sleep(1000); }
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised" + e.Message);
}
} }
}
myProcess = Process.Start("NotePad.exe");
myProcess.Exited = new EventHandler(Process_Exited);private void Process_Exited(object sender, EventArgs e)
{
System.Diagnostics.Process p = (sender as System.Diagnostics.Process);
Console.WriteLine(p.PagedMemorySize);
Console.WriteLine(p.ExitTime);
}
在callback里面set某个event
然后在main process中检查event的状态。