12-27-2011 03:32 PM
I have very little experience using the .NET palette in Labview; moreover, not much .NET programming experience in general.
Anyway, I'd like to be able to get the CPU % usage and have found that the performanceCounter class is a good place to start. I'm using a constructor with categoryName = Processor, counterName = % Processor Time, and instanceName = _Total. I then place the NextValue method in a while loop to get/chart the value (see attachment). If I set a delay in the while loop around 100 ms or greater, it seems to track the task manager CPU performace fairly well. If I set the delay less than 100 ms, it starts getting off track.
The closer it is to zero, it seems to only read NextValue = 0, 50, or 100. Neither occurs more than the other. I know running the loop faster should cause a higher CPU usage, but CPU usage and delay time don't seem to follow any type of curve.
I know this is a counter value of some sort. And, I understand NextValuereturns a "calculated" value. So, my question is, where can I find some info about how fast to read NextValue method of the performanceCounter class.
I'm also looking into doing it myself. That is, read NextSample and find a formula to calculate it directly. Perhaps this coupled with other methods would be more efficient?
PLEASE excuse any misuse of terminology when referring to OOP. I hope my meaning is clear.
Any help/insight is appreciated. Thanks!
12-28-2011 04:21 PM
Hello, dnorwood!
It looks like your program is actually working fine.
It sounds like you're observing the result of a loop process. When the looped VI is the only task that your processor is actively running, it will (practically) only observe usage in relation to that task. Since the VI contains a loop, it will effect CPU usage in the same way, at the same time, for each iteration of the loop.
Thus, when you read values, the reads occur at the same time each iteration due to your wait function. The loop also runs in the same way each iteration, and thus the same amount of PC usage is observed at the same time each iteration.
For instance, try running the VI at, say, 20ms to wait. You'll notice recurring values, but likely different from 100 50 or 0%. If you then open a new program (say, Firefox or MS Word), you should see many different values while the program opens (and CPU usage is changing), before it again settles.
There is a reason you only notice this at lower "ms to wait" values - the program's usage is likely similar to that from other programs if it is running slowly (ie above 100ms). When the value is lowered, the program runs faster and thus takes up more of the CPU usage, and the non-active tasks (such as an already open browser or word file) become negligible.
Try, for instance, setting the "ms to wait" to 5 or below. You'll see this rails your CPU, and only 100 or 0% usage is read. The 50% usage you see at
Please let me know if you have any more questions on this! I'll be happy to help.
Best,