473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Getting WMI properties from Win32_PerfFormattedData_PerfProc_Process class when searching by IDProcess

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
Nov 16 '05 #1
3 8506
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 :)

"Christopher Attard" <ch*********@gfi.com> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
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
Nov 16 '05 #2
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.

"Christopher Attard" <ch*********@gfi.com> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
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
Nov 16 '05 #3
Ok, problem solved. 10x to both of you.

Chris
"Christopher Attard" <ch*********@gfi.com> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
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
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Thanos Tsouanas | last post by:
Hello. I would like to have a quick way to create dicts from object, so that a call to foo would return obj.bar. The following works, but I would prefer to use a built-in way if one exists. ...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
1
by: Hardy Wang | last post by:
Hi, I am using DirectoryEntry to access IIS properties. After searching MSDN, I get "The Internet Information Services (IIS), and WinNT providers do not currently support Count, so each will throw...
4
by: Christopher Attard | last post by:
Hi, I'm writing a c# application which queries the WMI class "Win32_PerfFormattedData_PerfProc_Process" for IDProcess, VirtualBytes and WorkingSet for each running process. When I try to enumerate...
5
by: mike | last post by:
Hi, I have been playing with VB.NET/C# for getting some general properties of a fileinfo object. However, FileInfo object does not seem to expose some of the basic properties like TYPE that used...
4
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String;...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
8
by: Andrzej Lipski | last post by:
I am new to dotnet and I'll tried searching Google for a solution to my problem. I am hoping that it is possible to do, or am I going down a dead end? I have a User class that has known...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.