Solved

Close program after period of inactivity

Posted on 2012-04-10
8
1,145 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:phantomcom
  • 5
  • 3
8 Comments
 
LVL 41

Expert Comment

by:pcelba
ID: 37829774
Bryan_123 posted a piece of code (http://www.experts-exchange.com/Microsoft/Applications/FoxPro/Q_21053578.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

0
 
LVL 41

Expert Comment

by:pcelba
ID: 37829779
To answer your second question: use Google to search EE.
0
 

Author Comment

by:phantomcom
ID: 37829958
Should i place the above code on the click event of my login form?

Dean
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 41

Expert Comment

by:pcelba
ID: 37830015
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.
0
 

Author Comment

by:phantomcom
ID: 37832476
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
0
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 37833626
You are closer now.

The declaration part should be executed just once so you should place the following commands into your app startup part:

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

Timer event should contain following code:
Local iTime As Integer, iLast As Integer
Local plii As String, cLast As String, totTime

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  && this constant must remain 1000

IF m.totTime > 20  && test for 20 secs 
  *-- Actions when the code waits longer than 20 sec
  THIS.Enabled = .F.  && Disable the Timer
  WAIT WINDOW "Inactive longer than 20 sec ... QUIT in 3 secs" TIME 3
  QUIT
ELSE
  WAIT WINDOW "Inactive " + TRANSFORM(m.totTime) + " sec" NOWAIT
ENDIF

Open in new window


To better see what's hapenning you may set the Timer.Interval to 1000 ms. You have to write your own code in at the end of the above code sample. The interval in the real application can be e.g. 5 minutes - no need to check inactivity every minute.

The Timer can remain disabled when no user is logged in. If the user logs in then you may enable the Timer by setting its Enabled property to .T.:

YourForm.YourTimer.Enabled = .T.
YourForm.YourTimer.Interval = ...
0
 

Author Comment

by:phantomcom
ID: 37835526
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.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 37836327
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Word Doc or PDF file to VFP FRX report 9 1,872
Translate dll vb.net code to VFP 9 10 714
Alter Cursor Results 4 143
Controlling printer trays from browser 3 159
Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question