Page 1 of 1
Automatic Process Manager
Posted: Thu Mar 29, 2012 3:26 pm
by StanDan
Hey guys,
Long story short, I need my PB app to run only while the computer is idle. Currently it runs as a scheduled task, every 15 minutes or so it checks for some work on the server and if it has some then it churns away at it. Any idea how to do this? I was thinking maybe read the process list and look for a screensaver? Maybe there's some kind of library that I can link to that already does it? Or a process manager.... I'm completely at a loss. Google doesn't seem to be helping and I can't find anything on the forum already.
Any ideas?
Re: Automatic Process Manager
Posted: Thu Mar 29, 2012 3:35 pm
by jesperbrannmark
The only way to really check for a screensaver for me has been to set a timer, and every time the timer runs set the current time in a variable. Next time the timer runs - look at the difference between these two numbers and if its greater than ... 20 minutes (in your case) then it means that the computer has been sleeping. Altert the user to turn off screen saver.
Re: Automatic Process Manager
Posted: Thu Mar 29, 2012 5:19 pm
by StanDan
Jesper: Thank you! That is an interesting idea. I'm more concerned in having my program run like a screen saver. In other words, I would like it to only run while the computer is idle. Something like this:
Code: Select all
If ComputerIsIdle()
CheckForWork()
Else
Delay(5000)
EndIf
How would I check to see if the computer is idle?
I was just thinking, maybe I could monitor the mouse position. So if the mouse isn't moving for 5 minutes then I can assume it's safe to run. Hmmm. It's an interesting problem because that would require my PB script to actively monitor the mouse even when it didn't have focus. And I would need to monitor the keyboard too, since maybe they're typing a letter or something. My program would probably look like a keylogger!

Re: Automatic Process Manager
Posted: Thu Mar 29, 2012 5:25 pm
by IdeasVacuum
Perhaps the goal should be more towards a low-level of use rather than completely idle. Is your app really so disruptive that it should not run even if the User is only typing a memo in Notepad?
Dectecting how busy the CPU is might be an option:
http://www.purebasic.fr/english/viewtop ... 13&t=46347
Re: Automatic Process Manager
Posted: Thu Mar 29, 2012 5:49 pm
by StanDan
I just realized that I might be going about this wrong, it turns out that scheduled tasks have this option already. So this just got a LOT easier. lol
http://www.microsoft.com/resources/docu ... x?mfr=true
I guess this is why nobody bothered to ask before. I was thinking of how to program this, but I think relying on the windows implementation is the best.
Re: Automatic Process Manager
Posted: Thu Mar 29, 2012 8:26 pm
by Thorium
You also can just set the process priority class to #IDLE_PRIORITY_CLASS and thread priority to #THREAD_PRIORITY_IDLE.
So the process could run all the time but only get's CPU time not needed by any other process.
Re: Automatic Process Manager
Posted: Fri Mar 30, 2012 7:47 am
by Shardik
Have you already taken a look into this old code example
from Rings in which he demonstrated how to detect when
the processor is entering idle state?
http://www.purebasic.fr/english/viewtopic.php?t=19712
Re: Automatic Process Manager
Posted: Fri Mar 30, 2012 10:33 am
by Derren
Code: Select all
Structure lastinputinfo
cbSize.i
dwTime.l
EndStructure
Define var.lastinputinfo
var\cbSize = SizeOf(LASTINPUTINFO)
Delay(1000)
GetLastInputInfo_(@var)
Debug GetTickCount_() - var\dwTime
Re: Automatic Process Manager
Posted: Sat Mar 31, 2012 11:52 pm
by StanDan
Thanks guys for the help, I decided to go with the option of letting windows do my task scheduling. Not sure if it isn't easier to just write it myself though, lol. Windows is being a total jerkface about adding a scheduled task from a Byteessentials Installer script. I may just end up writing it myself and using one of these suggestions.
