Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Checking that local Windows machine is idle

by Anonymous Monk
on Feb 04, 2001 at 00:13 UTC ( #56246=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to, in perl, see if the win32 computer a perl script is running on is "idle" ? I know there has got to be some sort of internal timer that controls the screensaver.


2006-03-15 Retitled by g0n, as per Monastery guidelines
Original title: 'Win32'

  • Comment on Checking that local Windows machine is idle

Replies are listed 'Best First'.
Re: Checking that local Windows machine is idle
by saucepan (Scribe) on Feb 04, 2001 at 07:14 UTC
    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);
Re: Checking that local Windows machine is idle
by Corion (Pope) on Feb 04, 2001 at 06:29 UTC

    If your program is to be run while the computer is idle, why not simply set your perl script as the screensaver under Windows ?

    A Win32 screensaver will be executed when the computer thinks so. For configuration, it will be started with the /c command line switch.

    You can then either use your script as the screensaver or let your script run all the time and just let the screensaver program tell your script that it should now run.

Re: Checking that local Windows machine is idle
by enoch (Chaplain) on Feb 04, 2001 at 04:27 UTC
    SysInternals makes a program called PMon that is free for download and will give you a process listing.
    open(LIST, "pmon|"); while(<LIST>) { # analyze output of pmon here... }
    Something like above should do the trick. I have never used PMon, so I don't know what the ouput looks like, but I would think it would give you a process's current state in the output.

Re: Checking that local Windows machine is idle
by InfiniteSilence (Curate) on Feb 05, 2001 at 19:04 UTC
    There is a pmon.exe in the resource kit support tools available here from Microsoft. These are the tools they either forgot to add in the standard resource kit or did not think anybody wanted.

    Celebrate Intellectual Diversity

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://56246]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-01-23 00:06 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (238 votes). Check out past polls.