111,093
社区成员




namespace test4
{
class ClassTest1
{
public static PerformanceCounter allPfc = new PerformanceCounter("Process", "% Processor Time", "_Total");
public int[] myRecord=new int[50];
private PerformanceCounter myPfc = new PerformanceCounter("Process", "% Processor Time");
public ClassTest1(string myInstance)
{
for (int i = 0; i < myRecord.Length; i++)
{
myRecord[i] = 0;
}
myPfc.InstanceName = myInstance;
}
public string GetMyPrc()
{
try
{
return ((myPfc.NextValue() * 100) / allPfc.NextValue()).ToString();
}
catch
{
return ("");
}
}
}
}
class MyTool
{
public static string GetProcessInstanceName(int pid)
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
string tempInst = "读取失败";
try
{
string[] instances = cat.GetInstanceNames();
foreach (string instance in instances)
{
using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
{
long val = cnt.RawValue;//RawValue得到是long类型数据!
if (val == pid)
{
tempInst = instance;
}
}
}
return tempInst;
}
catch (Exception ex)
{
return tempInst;
}
}
}