PerformanceCounter (CPU load) staggers for 20 seconds
From: Pete (Pete_at_discussions.microsoft.com)
Date: 01/26/05
- Next message: Steven Cheng[MSFT]: "RE: Shell_NotifyIcon from a service?"
- Previous message: Flip: "Re: how do you get current username in aspx?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 25 Jan 2005 22:45:03 -0800
When using PerformanceCounter.NextValue to measure CPU load (percentage), it
returns quickly on every call but then takes 20 seconds at the 2 minute
boundary. After this it again returns quickly. Here's a sample:
//**********************************
using System.Diagnostics;
using System.Threading;
PerformanceCounter pc = new PerformanceCounter("Processor",
"% Processor Time", "_Total");
pc.NextValue(); // prime (values are relative to last)
DateTime now = DateTime.Now;
for (int i = 0; i < 240; ++i) { // it staggers at i == 120
int start = Environment.TickCount;
decimal load = decimal.Round((decimal) pc.NextValue(), 0);
int end = Environment.TickCount;
Console.WriteLine("Period: {0}, Load: {1}, Delta: {2}ms",
DateTime.Now - now, load, end - start);
Thread.Sleep(1000);
}
//**********************************
This code displays the time it takes in milliseconds for each call to
PerformanceCounter.NextValue to measure CPU load as a percentage. It
typically takes 0ms for the first 119 calls. Then at 01:59 it takes 20
seconds. After that it again returns quickly. I'm seeing this on three
different systems (XP Home, XP Pro, Server 2003), and using .NET 1.1 and .NET
2.0 Beta2, both debug and release builds. Has anyone else had problems with
this issue?
In production code I'm calling PerformanceCounter.NextValue from a separate
thread, but it still seems to "freeze" ALL currently running threads of my
app - nice 20 second delay :-)
It only seems to happen at the 2 minute boundary, after that it doesn't
(seem to) happen again.
Thanks for any help.
- Next message: Steven Cheng[MSFT]: "RE: Shell_NotifyIcon from a service?"
- Previous message: Flip: "Re: how do you get current username in aspx?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|