Getting System Idle Time


I got this working example from EE to wait for User input (keyboard and Mouse):

Local iTime As Integer, iLast As Integer
Local plii As String, cLast As String

Declare Integer GetTickCount In kernel32
Declare Short GetLastInputInfo In win32API String @ plii

iTime      = GetTickCount()
plii      = Chr(8) + Replicate(Chr(0), 7)
cLast      = GetLastInputInfo(@plii)
iLast      = (Asc(Substr(plii, 8, 1)) * 16777216) + ;
(Asc(Substr(plii, 7, 1)) * 65536) + ;
(Asc(Substr(plii, 6, 1)) * 256) + ;
Asc(Substr(plii, 5, 1))

iTime is the milliseconds your computer has been on.
iLast is the milliseconds your computer was on when your last keystroke or mousemove was.

To get minutes idle:
TotTime=(m.iTime - m.iLast) / 1000

The above example checks only user inputs (Keyboard and Mouse) but as per MS Article the system is idle only if the following conditions are not true:

The System Idle Task Scheduler service monitors for the system to be idle. The system is considered to be idle if for the last 10 minutes:

There is no user input.
The CPU and disk usage is less than 10 percent.

Question: My question is that how I know if the system is idle in Visual Foxpro based on the above conditions (CPU and Disk Usages)?


LVL 35
Nirmal SharmaSolution ArchitectAsked:
Who is Participating?
pcelbaConnect With a Mentor Commented:
I didn't write "loop is not necessary"...  I wrote continuous loop is not necessary...

The loop is necessary but you may wait in each pass, so the CPU loading will be minimal.

Following example calculates CPU loading based on GetSystemTimes. You may check CPU loading for continuous loop or when waiting for a key press by INKEY (just un/comment appropriate lines):


DECLARE integer GetSystemTimes IN "kernel32.dll" string @, string @, string @

t1 = SECONDS()
a1 = repl(CHR(0),8)
b1 = repl(CHR(0),8)
c1 = repl(CHR(0),8)
? GetSystemTimes(@a1,@b1,@c1)
a1 = str2bi(a1)
b1 = str2bi(b1)
c1 = str2bi(c1)
t2 = SECONDS()
a2 = repl(CHR(0),8)
b2 = repl(CHR(0),8)
c2 = repl(CHR(0),8)
? GetSystemTimes(@a2,@b2,@c2)
a2 = str2bi(a2)
b2 = str2bi(b2)
c2 = str2bi(c2)
st = (b2-b1)+(c2-c1)

? "Waiting period: ", t2-t1, "s"
? "CPU usage %: ", (st-(a2-a1))/st*100
*? a2-a1
*? b2-b1
*? c2-c1


LOCAL lnOut, laPower256[8]
lnOut = 0
laPower256[1] = 1
laPower256[2] = 256
laPower256[3] = 256*256
laPower256[4] = 256*256*256
laPower256[5] = 256*256*256*256
laPower256[6] = 256*256*256*256*256
laPower256[7] = 256*256*256*256*256*256
laPower256[8] = 256*256*256*256*256*256*256

FOR lnI = 1 TO 8
  lnOut = lnOut + ASC(SUBSTR(lcString, lnI, 1))*laPower256[lnI]


Open in new window

Nirmal SharmaSolution ArchitectAuthor Commented:
and also what I have obsereved is that CPU is taking at least 80% when I run the above code with a Do While loop.

Please advise.

It is not necessary to check keyboard hits and mouse moves continuously in a loop. To decrease CPU loading significantly during the loop you have to insert some waiting command into the loop, e.g.:

= INKEY(0.5)

CPU Usage/Idle time is available in GetSystemTimes API routine:
Unfortunately, i have no info about Disk usage API and you should ask this specific question in some operating system zone.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Nirmal SharmaSolution ArchitectAuthor Commented:
Thanks for your response!

If it is not necessary to check using a loop then how I check for user input without a loop? is there any other mechanism to do so?

Could you please give me an example how I leverage GetSystemTimes with the above example?

Appreciate your help!

Nirmal SharmaSolution ArchitectAuthor Commented:
Thank You
Much appreciated!
Nirmal SharmaSolution ArchitectAuthor Commented:
Hi (sorry I don't have enough points to ask a small query in separate question but it's urgent)

Is there any way to cover a user's desktop using any command in VF9?

For example:

IF asds="Y"
   define wind mn1 from -03,-03 to 1000,1000 IN DESKTOP
   acti wind mn1

the above code works fine except sometimes the program is minized to the taskbar. Therefore, user has to click on Taskbar program (Application) get desktop covered again.

Is there any way that if asds="Y" then user's desktop should be covered with wind mn1?


You could try ZOOM WINDOW mn1 MIN
or  _screen.WindowState = 2
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.