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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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. ...
|
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...
|
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...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |