Automatic Process Manager

Just starting out? Need help? Post your questions and find answers here.
StanDan
User
User
Posts: 57
Joined: Sun Feb 26, 2006 3:43 am
Location: Missouri, United States

Automatic Process Manager

Post 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?
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Automatic Process Manager

Post 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.
StanDan
User
User
Posts: 57
Joined: Sun Feb 26, 2006 3:43 am
Location: Missouri, United States

Re: Automatic Process Manager

Post 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! :lol:
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Automatic Process Manager

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
StanDan
User
User
Posts: 57
Joined: Sun Feb 26, 2006 3:43 am
Location: Missouri, United States

Re: Automatic Process Manager

Post 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.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Automatic Process Manager

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Automatic Process Manager

Post 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
User avatar
Derren
Enthusiast
Enthusiast
Posts: 316
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Automatic Process Manager

Post 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
StanDan
User
User
Posts: 57
Joined: Sun Feb 26, 2006 3:43 am
Location: Missouri, United States

Re: Automatic Process Manager

Post 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. :)
Post Reply