Click here to Skip to main content
15,905,683 members
Home / Discussions / C#
   

C#

 
AnswerRe: formating numbers from strings PinPopular
Scott Dorman17-Sep-08 5:06
professionalScott Dorman17-Sep-08 5:06 
AnswerRe: formating numbers from strings Pin
Blue_Boy17-Sep-08 9:31
Blue_Boy17-Sep-08 9:31 
QuestionSoftware Design.. Whats your approach? Pin
JPWheeler17-Sep-08 3:53
JPWheeler17-Sep-08 3:53 
AnswerRe: Software Design.. Whats your approach? Pin
Scott Dorman17-Sep-08 4:41
professionalScott Dorman17-Sep-08 4:41 
AnswerRe: Software Design.. Whats your approach? Pin
PIEBALDconsult17-Sep-08 6:34
mvePIEBALDconsult17-Sep-08 6:34 
AnswerRe: Software Design.. Whats your approach? Pin
netJP12L17-Sep-08 7:33
netJP12L17-Sep-08 7:33 
AnswerRe: Software Design.. Whats your approach? Pin
Pete O'Hanlon17-Sep-08 9:20
mvePete O'Hanlon17-Sep-08 9:20 
QuestionHow to get the cpu percentage of a specific process using WMI with C# ? Pin
icks17-Sep-08 3:23
icks17-Sep-08 3:23 
Hi,

I want to monitor the health of a process on a Windows computer (XP, Server 2003) using WMI, and for this I want to get it's processor usage in percent (same as process listed in the Task Manager with CPU percentage) with using C# (> 2.0).

I tried naively to use the Win32_PerfFormattedData_PerfOS_Processor classes using the PercentProcessorTime property but this gives me only 0% or 100% values.

I red on forums that a calculation must be done to get the correct percentage and found this VB script below (http://msdn.microsoft.com/en-us/library/aa394597(VS.85).aspx).

I tried to apply this code into the following C# code with the help of some article of The Code Project, but without success as I have strange values out for the Timestamp_Sys100NS property (null, big numbers that give wrong calculation).

Many thanks for your help.



VB scrip code :

Set object1 = objWMIService.Get( _
"Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N1 = object1.PercentProcessorTime
D1 = object1.TimeStamp_Sys100NS
Wscript.Sleep(1000)
set object2 = objWMIService.Get( _
"Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
N2 = object2.PercentProcessorTime
D2 = object2.TimeStamp_Sys100NS

' CounterType - PERF_100NSEC_TIMER_INV
' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100



C# code :

mgmtScope = new ManagementScope("\\\\" + hostName + "\\root\\cimv2");
try {
mgmtScope.Connect();
}

...

string searchQuery = "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE Name = 'MyProcess'";
ObjectQuery oq = new ObjectQuery(searchQuery);
ManagementObjectSearcher query = new ManagementObjectSearcher(mgmtScope, oq);
try {
ManagementObjectCollection servicesCollection = query.Get();
foreach (ManagementObject service in servicesCollection) { // seems not possible to do servicesCollection[0]
percentProcessorTime = double.Parse(service.Properties["PercentProcessorTime"].Value.ToString());
}
}
...
string searchQuery = "SELECT * FROM Win32_PerfRawData_PerfOS_Processor WHERE Name = 'MyProcess'";
ObjectQuery oq = new ObjectQuery(searchQuery);
ManagementObjectSearcher query = new ManagementObjectSearcher(mgmtScope, oq);
try {
ManagementObjectCollection servicesCollection = query.Get();
foreach (ManagementObject service in servicesCollection) {
timeStampSysNS100 = double.Parse(service.Properties["Timestamp_Sys100NS"].Value.ToString());
}
}
...
double d1 = this.getPercentProcessorTime();
double n1 = this.getTimeStampSysNS100();
Thread.Sleep(1000);
double d2 = this.getPercentProcessorTime();
double n2 = this.getTimeStampSysNS100();

double percentPT = (1 - ((n2 - n1) / (d2 - d1))) * 100;
string res = "Process CPU usage : " + percentPT;
AnswerRe: How to get the cpu percentage of a specific process using WMI with C# ? Pin
Mark Salsbery17-Sep-08 7:34
Mark Salsbery17-Sep-08 7:34 
JokeRe: How to get the cpu percentage of a specific process using WMI with C# ? Pin
DaveyM6917-Sep-08 9:40
professionalDaveyM6917-Sep-08 9:40 
GeneralRe: How to get the cpu percentage of a specific process using WMI with C# ? Pin
Mark Salsbery17-Sep-08 9:48
Mark Salsbery17-Sep-08 9:48 
GeneralRetrieving Text from Text Boxes Dynamically Generated at RunTime Pin
dboy22117-Sep-08 3:02
dboy22117-Sep-08 3:02 
Questionwindows service - timeout Pin
arkiboys17-Sep-08 2:48
arkiboys17-Sep-08 2:48 
AnswerRe: windows service - timeout Pin
PIEBALDconsult17-Sep-08 4:02
mvePIEBALDconsult17-Sep-08 4:02 
GeneralRe: windows service - timeout Pin
arkiboys17-Sep-08 21:27
arkiboys17-Sep-08 21:27 
GeneralRe: windows service - timeout Pin
PIEBALDconsult18-Sep-08 4:44
mvePIEBALDconsult18-Sep-08 4:44 
QuestionCSV File Pin
nhss17-Sep-08 2:33
nhss17-Sep-08 2:33 
AnswerRe: CSV File Pin
J4amieC17-Sep-08 2:38
J4amieC17-Sep-08 2:38 
JokeRe: CSV File Pin
Giorgi Dalakishvili17-Sep-08 2:51
mentorGiorgi Dalakishvili17-Sep-08 2:51 
GeneralRe: CSV File Pin
Scott Dorman17-Sep-08 3:09
professionalScott Dorman17-Sep-08 3:09 
GeneralRe: CSV File Pin
Muammar©17-Sep-08 3:20
Muammar©17-Sep-08 3:20 
GeneralRe: CSV File Pin
Ashfield17-Sep-08 3:40
Ashfield17-Sep-08 3:40 
AnswerRe: CSV File Pin
Mycroft Holmes17-Sep-08 20:00
professionalMycroft Holmes17-Sep-08 20:00 
QuestionHelp with unsafe code ( array of pointer to a stucture) [modified] Pin
Preetham.N17-Sep-08 2:24
Preetham.N17-Sep-08 2:24 
AnswerRe: Help with unsafe code ( array of pointer to a stucture) Pin
Alan Balkany17-Sep-08 3:23
Alan Balkany17-Sep-08 3:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.