views:

134

answers:

1

I'm trying to access the "TimeRemaining" value for the power management idle counter. Google search indicates that lots of folks (including me) can get a value once, but every subsequent call gives the same results. No countdown, no change in CurIdle value...

Here's a short version of the code in question:

#include <windows.h>
#include <tchar.h>
#include <powrprof.h>
typedef struct _SYSTEM_POWER_INFORMATION {
  ULONG MaxIdlenessAllowed;
  ULONG Idleness;
  ULONG TimeRemaining;
  UCHAR CoolingMode;
}SYSTEM_POWER_INFORMATION, *PSYSTEM_POWER_INFORMATION;

int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEM_POWER_INFORMATION spin;
    CallNtPowerInformation(SystemPowerInformation,NULL,0,&spin,sizeof(SYSTEM_POWER_INFORMATION));
    _tprintf(_T("MaxIdleness\t%d\t"),spin.MaxIdlenessAllowed);
    _tprintf(_T("CurIdle\t%d\t"),spin.Idleness);
    _tprintf(_T("TimeRemaining\t%d\n"),spin.TimeRemaining);
    return 0;
}

When I run this from the command line, I always get the same values for MaxIdleness (which is to be expected), CurIdle (which should change) and TimeRemaining (which is always at the maximum value...understandable since I'm executing from the command line, but I get the same result if I put it in a batch file with sleep.exe in between or in a program that iterates with a "sleep" between each call to CallNtPowerInformation).

Can anyone send me a copy of example code that does show varying TimeRemaining and CurIdle values?

A: 

As sometimes happens, I solved my problem. Here's what I learned for those who come across the same behavior:

1) My specific problem was, as one might have guessed, that one of the programs loaded on startup was somehow resetting the counter frequently...so the counter was always equal to the maximum value. I manually disabled (or uninstalled) programs until the PC would suspend again.

2) The construct I suggested in my question above (simple console program with sleep.exe in a batch file) still doesn't work. My guess is that either sleep.exe or any batch file executing keeps the counter at the maximum value.

3) The other thing I noted (once my test PC was properly suspending) was that the counter and other values (MaxIdle, etc.) are only updated every 15 seconds. I don't know if this is true for all WinXP installations, but it helps to explain why folks like me that were looking for a second-by-second countdown weren't able to get it. (It also explains why my testing my CallNtPowerInformation timer on other programs that did suspend properly didn't seem to work...after I got the same value repeated every second, I must have given up before 15 seconds had elapsed.)

4) So...to summarize: a) be very careful...lots of things will reset the Idle timer b) don't expect it (or CurIdle or MaxIdleness) to be updated more than every 15 seconds if the PC is really idle (if it's not, the counter is reset immediately to the maximum value).

I hope this helps others trying to use this function.

dscottm

dscottm