Link to home
Start Free TrialLog in
Avatar of phantomcom
phantomcomFlag for United States of America

asked on

Close program after period of inactivity

In my business i potentially have instances of my program open on various networked computers.  I would like to have the instances "Time out" if there was no keyboard or mouse activity after a certain period of time, say 15 or 30 minutes and return the program back to the login screen.  Keep in mind i need it as simple as possible (i'm a still a bit of a newbie).

Unfortunately i don't like the new experts exchange website and find it hard to search for just foxpro solutions. (Maybe someone can also clue me in on how to do that more easily).

Sincerely,
Dean
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Bryan_123 posted a piece of code (https://www.experts-exchange.com/questions/21053578/Determining-system-idle-time-using-GetLastInputInfo-and-LastInputInfo.html) which you can use in a timer to determine the last user input and do appropriate action:
CLEAR

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

DO WHILE .t.
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))

TotTime=(m.iTime - m.iLast) / 1000

*? m.iTime, m.iLast, totTime
? "Inactivity TIME:", totTime, "s"

lcKey=INKEY(0.5)
IF lcKey = 27  && Esc key
  EXIT
ENDIF

ENDDO

Open in new window

To answer your second question: use Google to search EE.
Avatar of phantomcom

ASKER

Should i place the above code on the click event of my login form?

Dean
No, not at all. The code is just an example how the idle time is calculated and you may use this code in Command window.

To incorporate the main part of the code (without the DO WHILE loop) into your application you should find the right place where you may define e.g. the timer.

You should check the inactivity time in the Timer event and if the inactive time is long enough then you should log the user out and display the login form.

The timer should be created on a form which is still active or visible or exists at least. It can even be the main FoxPro window referenced by _screen variable.

Implementation details depend on your app architecture.
i added the timer object to the most appropriate form. I changed the interval within the timer object to 20000 milliseconds just for testing purposes. Do i now place the above code in the timer procedure? (Remember i'm a bit of a newbie still).

Here's what i added under the timer procedure:

CLEAR

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))

TotTime=(m.iTime - m.iLast) / 20000

*? m.iTime, m.iLast, totTime
? "Inactivity TIME:", totTime, "s"

lcKey=INKEY(0.5)
IF lcKey = 27  && Esc key
  EXIT
ENDIF

Pretty much the same as the above code. After 20 seconds the cursor disappears but nothing else happens. If i move the mouse the cursor reappears.

What's next to get this to work.

Dean
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Horray! It appears to work. Just to make sure i understand correctly the "IF m.totTime > 20" the 20 is seconds so at this point if i want it to wait 15 minutes before timeout i would set it for 900 seconds.

Currently the code shuts down (quits) the whole program. How can i get it to close the current form and return to the login screen which is login.scx.
Yes, 900 is 15 minutes.
To answer your question how to display the login screen is more difficult because it depends on your app architecture. You'll probably need to log the current user off, so you have to call the routine used for log-off process. Then you may display the log-in form as you do when the application starts. The simplest command is "DO FORM login" but each application handles the form creation in a different way so you will need to study your code a bit.