Click here to Skip to main content
15,922,166 members
Home / Discussions / C#
   

C#

 
GeneralRe: Determining type compatiblity Pin
Clive D. Pottinger17-Sep-08 9:51
Clive D. Pottinger17-Sep-08 9:51 
QuestionreSetting old values Pin
netJP12L17-Sep-08 7:30
netJP12L17-Sep-08 7:30 
AnswerRe: reSetting old values Pin
Jimmanuel17-Sep-08 9:26
Jimmanuel17-Sep-08 9:26 
Questionbrush problem... Pin
Sajjad Izadi17-Sep-08 6:30
Sajjad Izadi17-Sep-08 6:30 
AnswerRe: brush problem... Pin
Syed Mehroz Alam17-Sep-08 7:04
Syed Mehroz Alam17-Sep-08 7:04 
GeneralRe: brush problem... Pin
Sajjad Izadi17-Sep-08 7:17
Sajjad Izadi17-Sep-08 7:17 
AnswerRe: brush problem... Pin
Harvey Saayman17-Sep-08 8:05
Harvey Saayman17-Sep-08 8:05 
Joke[ot] Pin
Guffa17-Sep-08 8:36
Guffa17-Sep-08 8:36 
GeneralRe: [ot] Pin
Harvey Saayman17-Sep-08 8:52
Harvey Saayman17-Sep-08 8:52 
Questionformating numbers from strings Pin
brsecu17-Sep-08 4:55
brsecu17-Sep-08 4:55 
AnswerRe: formating numbers from strings Pin
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 

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.