Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Checking that local Windows machine is idle

by saucepan (Scribe)
on Feb 04, 2001 at 07:14 UTC ( #56271=note: print w/replies, xml ) Need Help??

in reply to Checking that local Windows machine is idle

This depends on what you mean by "idle." The screensaver will fire up when there have been no keyboard or mouse events for N minutes, even if the CPU is pegged at 100% at the time (by the client, say).

If the screensaver's idea of idle matches your requirements, you could do a lot worse than to take Corion's advice and just install your script as the screensaver.

Alternately, you could write a wrapper script that uses Win32::Process to launch your real script with IDLE_PRIORITY_CLASS; then, you know the CPU is idle because when it isn't, your code isn't running. :)

If that's too kludgey, you can wade into Win32::API and figure out how to call SetPriorityClass (or maybe SetThreadPriority) from perl (both are found in kernel32.dll.. the docs I've got suggest that the HANDLE arg isn't optional, so you'll need to find a way to get hold of your own proc/thread handle to use these calls.)

If you are really feeling adventurous, you can query for a variety of performance counters using Win32::PerfLib (assuming WinNT or Win2k, mind you).

update: It would be cool if POSIX::nice would work -- it being portable and all -- but it isn't implemented on ActivePerl 623 on my Win98 box here at home. Anyone know if it works on NT or Win2k?

update: If you decide to use Win32::API, here's the rest of the info you'll need:

#define IDLE_PRIORITY_CLASS 0x00000040 HANDLE GetCurrentProcess(VOID); BOOL SetPriorityClass(HANDLE hProcess, DWORD dwPriorityClass); #define THREAD_PRIORITY_LOWEST -2 HANDLE GetCurrentThread(VOID); BOOL SetThreadPriority(HANDLE hThread, int nPriority);

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://56271]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2023-03-20 16:05 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.