Results 1 to 13 of 13

Thread: idle or time out

  1. #1

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    43

    idle or time out

    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

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: idle or time out

    Not sure this is the professional way. but you can use this trick for time out.

    A timer will not going to support 36000000 ml (10X60X60X1000).
    so

    for a 10 Min Time Out:-

    place a timer in the form. put the interval 1000 (1 Min.)
    put this code.

    Option Explicit
    Private tm As Long

    Private Sub Timer1_Timer()
    tm = tm + 1
    If tm = 36000 Then
    'exit code here
    End If
    End Sub
    OR you can do other wise with tm=36000 ; tm=tm -1 to count down. check at tm=0

    Attached Files Attached Files
    Last edited by Fazi; Jul 10th, 2007 at 11:24 PM. Reason: 1sec. should be 1min on line 'place a timer in the form. put the interval 1000 (1 Min.)'

  3. #3

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    43

    Re: idle or time out

    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?

  4. #4
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: idle or time out

    you mean you need an event in your application to start time out count?

  5. #5

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    43

    Re: idle or time out

    I Mean how to start timer when the application on idle. not from form on load cause if some one still use the application the timer will not start

  6. #6
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: idle or time out

    Yah, i undestand. I am not sure how to.. let see is there any professional ways to count the idle session of a vb application

  7. #7
    Fanatic Member drivenbywhat's Avatar
    Join Date
    Jan 2007
    Location
    VA - USA
    Posts
    866

    Re: idle or time out

    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.

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: idle or time out

    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

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: idle or time out

    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.

  10. #10

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    43

    Re: idle or time out

    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.

  11. #11
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: idle or time out

    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.

  12. #12

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    43

    Re: idle or time out

    thanks for All, Now I understand.

  13. #13
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: idle or time out

    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.
    Last edited by rory; Oct 2nd, 2007 at 09:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width