Instant CPU Utilization Value
-
Does anyone know if there's a painless way to get an instantaneous reading for CPU Utilization? I realise I can use a PerformanceCounter to call .RawValue but I have no idea how to convert that to anything that means anything. I don't want to have to take 2 values (ie. using .NextValue or .NextSample on the performance counter) as I know how to do this (see the example code below)
Dim myCounter As System.Diagnostics.PerformanceCounter = New \_ System.Diagnostics.PerformanceCounter() myCounter.CategoryName = "Processor" myCounter.CounterName = "% Processor Time" myCounter.InstanceName = "\_Total" Console.WriteLine(myCounter.NextValue().ToString()) System.Threading.Thread.Sleep(1000) Console.WriteLine(myCounter.NextValue().ToString())
I realize that the CPU utilization value is a sampled value and *not* an instantaneous one but I'm thinking there must be a way to query whatever value Task Manager currently has - but I can't see a way of doing that? (If you're wondering, I have a time critical app that I'd like to record a timestamp with current CPU Utilization in a log - the value doesn't have to be perfect but having a value is better than not having one.) Thanks!
-
Does anyone know if there's a painless way to get an instantaneous reading for CPU Utilization? I realise I can use a PerformanceCounter to call .RawValue but I have no idea how to convert that to anything that means anything. I don't want to have to take 2 values (ie. using .NextValue or .NextSample on the performance counter) as I know how to do this (see the example code below)
Dim myCounter As System.Diagnostics.PerformanceCounter = New \_ System.Diagnostics.PerformanceCounter() myCounter.CategoryName = "Processor" myCounter.CounterName = "% Processor Time" myCounter.InstanceName = "\_Total" Console.WriteLine(myCounter.NextValue().ToString()) System.Threading.Thread.Sleep(1000) Console.WriteLine(myCounter.NextValue().ToString())
I realize that the CPU utilization value is a sampled value and *not* an instantaneous one but I'm thinking there must be a way to query whatever value Task Manager currently has - but I can't see a way of doing that? (If you're wondering, I have a time critical app that I'd like to record a timestamp with current CPU Utilization in a log - the value doesn't have to be perfect but having a value is better than not having one.) Thanks!
Well the painless solution would be to jump over the WinAPI cliff and plunge into the fiery depths of Microsoft Windows and trudge through the mountains of commands until you find the correct solution. Or you could google, or you could wait for another more useful response :)
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
-
Well the painless solution would be to jump over the WinAPI cliff and plunge into the fiery depths of Microsoft Windows and trudge through the mountains of commands until you find the correct solution. Or you could google, or you could wait for another more useful response :)
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
Thanks for the answer Thomas. I wanted to trudge through the WinAPI to find something but even with googling didn't really find a good place to start. I ended up coming up with a tiny service app that runs on the computer and tracks CPU Utilization using the counter sampling then responds on a tcp port to a request for the current value. Probably overkill but it beat not having a solution :)