This is an anwser for the updated question where the CPU usage is always zero.
Many
PerformanceCounter Class (System.Diagnostics)[
^] values depend on two reads. That means after a
PerformanceCounter
instance has been created,
PerformanceCounter.NextValue Method (System.Diagnostics)[
^] should be called once after creation ignoring the returned value. Following calls will then return the value for the time between the calls.
So you have to make your
CPUCounter
a static member of a class (e.g. your app class), initialise it, and call
NextValue()
once. Then further
NextValue()
calls should return non zero.
See also the example code of the above MSDN links and this note in the
NextValue()
documentation:
Quote:
If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.