Hi any body, can you tell me ho to make time out code?
if some body not use the application in 10 minute, the application will logout automatically and save all they work. And if they forgot to logout the application, user can loggin again in 1 hours with out reset.
Thanks for your information, but I need the time out running if some one does not use that application? so look like expire session.
can u tell me to to start that?
Well, I'm no professional (read the signature) but I think it could be done with just one timer. What does your app do anyways? Is it like a text editor where people are going to be pressing keys? The timer should load up on start and set to how many idle minutes have passed. Then, in the KEYPRESS event, everytime a key is pressed, disable that timer and enable it again. You could also make the TM static variable in the timer event that Fazi said to do, into a public variable. In the keypress event, you would reset it to 0 so it doesn't keep the info.
[vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.
heh, two threads on the front page asking for the same thing. Is school in session?
Add a timer control to a form, set the form's KeyPreview to True, then add this to the form's module:
Code:
Option Explicit
Private mdtmLastActivity As Date
Private Sub Form_Load()
mdtmLastActivity = Now()
Timer1.Interval = 1000 ' Not needed; just set property in design view
Timer1.Enabled = True ' Not needed; just set property in design view
End Sub
Private Sub Timer1_Timer()
If DateDiff("s", mdtmLastActivity, Now()) > 600 Then Unload Me
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
mdtmLastActivity = Now()
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
mdtmLastActivity = Now()
End Sub
Here is the other thread I mentioned, and it contains some discussion as to the merits of comparing against the start time (as my solution does) versus counting the elapsed time.
thanks for all, and how about if the user not use key press, but the use click or mouse over? so can we use that code together on that event, and how about the other question, that if user close the app without logout, user cannot use their username for a few minute.
Mouse activity is accounted for with the MouseMove event. (Included in the original code I posted.) Oddly enough, mouse clicks fire off MouseMove events even if the mouse isn't moving.
As for the other question, that's more involved since it requires persisting data outside the application. Does your application use a database, and does it have a user table? If so, add a Lockout datetime field to the user table. Then add / edit the following:
Code:
' In module
Sub Main()
Dim varLockout As Variant
Dim blnLockout As Boolean
' Load user's Lockout field to varLockout
If Not IsNull(varLockout) Then blnLockout = (Now() < varLockout)
If blnLockout Then
MsgBox "Locked out due to improper exit.", vbInformation, "Notice"
Else
Form1.Show
End If
End Sub
Public Sub CloseApp(Optional pblnLockout As Boolean)
Dim frm As Form
If pblnLockout Then
' Set user's Lockout field to DateAdd("s", 3600, Now())
Else
' Set user's Lockout field to Null
End If
For Each frm In Forms
Unload frm
Set frm = Nothing
Next
End Sub
' In form
Private Sub Timer1_Timer()
If DateDiff("s", mdtmLastActivity, Now()) > 600 Then
' Save their work
CloseApp True
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
CloseApp
End Sub
Last edited by Ellis Dee; Jul 11th, 2007 at 02:14 PM.
I know this is a couple months old but i whipped this up tonight for a project im working on and it might be able to help someone else ..
Tracking user idle and active on the system using a simple timer.
Some additional properties and procedures were also put in the class file, or made public, just incase one needed them for some reason; however they are optional. The procedures in the main form is all that is required for it to work.
If someone can make it better, clean it up, etc, please do ... and post it back here Im thinking maybe could use subclassing instead of a timer, but im not good in that area.