Getting WMI properties from Win32_PerfFormattedData_PerfProc_Process class when searching by IDProce

C

Christopher Attard

Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 
S

SnakeS

Hi,

public static void GetCpuUsage(string sIDProcess)

{

try

{

double d1,d2,n1,n2;

ManagementObjectSearcher searcher =

new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfProc_Process");


foreach(ManagementObject buf in searcher.Get())

if(buf["IDProcess"].ToString() == sIDProcess)

{

n1 = Convert.ToDouble(buf["PercentProcessorTime"]);

d1 = Convert.ToDouble(buf["TimeStamp_Sys100NS"]);

Console.WriteLine("IDProcess: " + buf["IDProcess"] +

" ProcTime: " + n1 + " TimeStamp: " + d1);

}



}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}

I don't say if this is the best method but I do so :)

bye :)



Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 
W

Willy Denoyette [MVP]

IDProcess is not a key attribute so you can't use it to create an instance of the class.
You should issue a select query using the IDProcess in the where clause like this:

select PercentProcessorTime, TimeStamp_Sys100NS from Win32_PerfFormattedData_PerfProc_Process where IDProcess = nnn

where nnn is the pid.

Willy.

Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 
C

Christopher Attard

Ok, problem solved. 10x to both of you.

Chris
Hi,
I'm trying to run this function which attempts to get 2 WMI properties from the specific WMI class for a process with a specified IDProcess. It's giving an "Invalid Object Path" exception. The function works when I search by process name (in this case WMI path should be: "Win32_PerfFormattedData_PerfProc_Process.Name='" +sProcessName +"'" ) but I need to search by IDProcess

public static void GetCpuUsage(string sIDProcess)

try

{

double d1,d2,n1,n2;

ManagementObject counter = new ManagementObject("Win32_PerfFormattedData_PerfProc_Process.IDProcess=" +sIDProcess);

n1 = Convert.ToDouble(counter["PercentProcessorTime"]);

d1 = Convert.ToDouble(counter["TimeStamp_Sys100NS"]);


}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}


Any suggestions about what might be wrong in this code? Thanks in advance.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top