Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > log off automatically
 

log off automatically

hi everyone i`m using Vb express 2008.

i have a question here, when user does not touch the program for a certain period of time,the application will log off automatically. Just like the online banking system,it will log off automatically for the user,if there no any changes for the application for period of time that have been set.

can anyone show me how to write this logic??

thank you.
simon tiong  Friday, March 27, 2009 9:48 AM

Hi Simon,

In my opinion, you can use a Timer in your application.Set the Interval of the Timer to a periodof time. Restart the timer if there's a user interaction. Sowhen the Timer ticks, i.e.there's no user interaction for the specified period of time, log off the application.

The key point here is how to detect theuser interaction in the application. I haven't thought out a good solution so far. My current thought is to capture Windows messagessent to the forms in the application. If there's no user interaction, the forms in the application won't receive any Winodws messages.

Hope this helps.

Sincerely,
Linda Liu

Linda Liu  Wednesday, April 01, 2009 10:05 AM
hi everyone i`m using Vb express 2008.

i have a question here, when user does not touch the program for a certain period of time,the application will log off automatically. Just like the online banking system,it will log off automatically for the user,if there no any changes for the application for period of time that have been set.

can anyone show me how to write this logic??

thank you.
I would use a timer for this logic. You can create it at design time (you can call it "logintimer") and set it's interval to however long your timeout duration needs to be. Then during your application's form_load event you can call logintimer.start()

Add these code snippets to your project. (edit: please ignore the <br/> tags that got mixed in with my snippets)
Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If My.Application.AdminAuthenticated = True Then 'if authenticated
            If loginModule.logintimer.Enabled = True Then 'and if timer is running
                loginModule.logintimer.Enabled = False
                loginModule.logintimer.Enabled = True
            End If
        End If
    End Sub
Private Sub logintimer_tick(ByVal sender As Object, ByVal e As EventArgs) Handles logintimer.tick
        lockthisapp()
        logintimer.Enabled = False
    End Sub


The first detects mouse movements on your form and resets your timer if there is movement (disable/enable = restart timer). The second one is the event that happens when your timer elapses. It contains a fictitious event called locktheapp(). This is where you would want to set your grids to readonly or turn off visibility to panels and buttons. The next line of code disables this timer so that it doesn't fire again in two minutes.

If you wanted to use the timer again you could set the logintimer.enable = true after a button click to log in or something like that.
----------------------------------------------------------------------------
Another route you could go is to set up a progressbar and count it down in reverse according to a couple of timers. This would allow your user to look up and know that the app is about to lock because the progress bar is almost empty.

to do that:
1. create progressbar at design time and set max value = 120 and step = -1 and visibility = false
2. create timer at runtime from a button click and set interval to 1000ms (1 second). (ex: logintimer = new timer() logintimer.interval = 1000) also set visible = true for progressbar
3. set the progressbar.value = your max value (so it looks filled)
4. in timer_tick event call the PreformStep() event of your progressbar and update tooltip for remaining seconds. If progressbar value = 0 then lock the app
Private Sub logintimer_tick(ByVal sender As Object, ByVal e As EventArgs) Handles logintimer.Tick
        If Not My.Forms.frmMain.pgAuthenticationTimeRemaining.Value = 0 Then  'if progress timer value not 0
            My.Forms.frmMain.pgAuthenticationTimeRemaining.PerformStep() 'decrement value
            My.Forms.frmMain.ToolTip.SetToolTip(My.Forms.frmMain.pgAuthenticationTimeRemaining, My.Forms.frmMain.pgAuthenticationTimeRemaining.Value & " seconds remain until lock.")
        Else
            TimeMeOut() 'if it's 0 then time us out
        End If
    End Sub

5. use this snippet for your form.mousemovement() event
Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If My.Application.AdminAuthenticated = True Then 'if authenticated
            If loginModule.logintimer.Enabled = True Then 'and if timer is running
                If Not Me.pgAuthenticationTimeRemaining.Value > 100 Then
                    Me.pgAuthenticationTimeRemaining.Value += 20  'extend for 20 more seconds
                End If
            End If
        End If
    End Sub


this snippet adds 20 to your progressbar.value when it detects mouse movement. It only adds if value is not more than 100.

I check for this because the max is 120 and if you go over your max you will toss an exception. Also, you don't want someone to be able to shake the mouse endlessly to refill the bar. Filling it back to a max of 90% is sufficient, I think.
Vegeta4ss  Wednesday, April 01, 2009 6:40 PM

Hi Simon,

In my opinion, you can use a Timer in your application.Set the Interval of the Timer to a periodof time. Restart the timer if there's a user interaction. Sowhen the Timer ticks, i.e.there's no user interaction for the specified period of time, log off the application.

The key point here is how to detect theuser interaction in the application. I haven't thought out a good solution so far. My current thought is to capture Windows messagessent to the forms in the application. If there's no user interaction, the forms in the application won't receive any Winodws messages.

Hope this helps.

Sincerely,
Linda Liu

Linda Liu  Wednesday, April 01, 2009 10:05 AM
hi everyone i`m using Vb express 2008.

i have a question here, when user does not touch the program for a certain period of time,the application will log off automatically. Just like the online banking system,it will log off automatically for the user,if there no any changes for the application for period of time that have been set.

can anyone show me how to write this logic??

thank you.
I would use a timer for this logic. You can create it at design time (you can call it "logintimer") and set it's interval to however long your timeout duration needs to be. Then during your application's form_load event you can call logintimer.start()

Add these code snippets to your project. (edit: please ignore the <br/> tags that got mixed in with my snippets)
Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If My.Application.AdminAuthenticated = True Then 'if authenticated
            If loginModule.logintimer.Enabled = True Then 'and if timer is running
                loginModule.logintimer.Enabled = False
                loginModule.logintimer.Enabled = True
            End If
        End If
    End Sub
Private Sub logintimer_tick(ByVal sender As Object, ByVal e As EventArgs) Handles logintimer.tick
        lockthisapp()
        logintimer.Enabled = False
    End Sub


The first detects mouse movements on your form and resets your timer if there is movement (disable/enable = restart timer). The second one is the event that happens when your timer elapses. It contains a fictitious event called locktheapp(). This is where you would want to set your grids to readonly or turn off visibility to panels and buttons. The next line of code disables this timer so that it doesn't fire again in two minutes.

If you wanted to use the timer again you could set the logintimer.enable = true after a button click to log in or something like that.
----------------------------------------------------------------------------
Another route you could go is to set up a progressbar and count it down in reverse according to a couple of timers. This would allow your user to look up and know that the app is about to lock because the progress bar is almost empty.

to do that:
1. create progressbar at design time and set max value = 120 and step = -1 and visibility = false
2. create timer at runtime from a button click and set interval to 1000ms (1 second). (ex: logintimer = new timer() logintimer.interval = 1000) also set visible = true for progressbar
3. set the progressbar.value = your max value (so it looks filled)
4. in timer_tick event call the PreformStep() event of your progressbar and update tooltip for remaining seconds. If progressbar value = 0 then lock the app
Private Sub logintimer_tick(ByVal sender As Object, ByVal e As EventArgs) Handles logintimer.Tick
        If Not My.Forms.frmMain.pgAuthenticationTimeRemaining.Value = 0 Then  'if progress timer value not 0
            My.Forms.frmMain.pgAuthenticationTimeRemaining.PerformStep() 'decrement value
            My.Forms.frmMain.ToolTip.SetToolTip(My.Forms.frmMain.pgAuthenticationTimeRemaining, My.Forms.frmMain.pgAuthenticationTimeRemaining.Value & " seconds remain until lock.")
        Else
            TimeMeOut() 'if it's 0 then time us out
        End If
    End Sub

5. use this snippet for your form.mousemovement() event
Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If My.Application.AdminAuthenticated = True Then 'if authenticated
            If loginModule.logintimer.Enabled = True Then 'and if timer is running
                If Not Me.pgAuthenticationTimeRemaining.Value > 100 Then
                    Me.pgAuthenticationTimeRemaining.Value += 20  'extend for 20 more seconds
                End If
            End If
        End If
    End Sub


this snippet adds 20 to your progressbar.value when it detects mouse movement. It only adds if value is not more than 100.

I check for this because the max is 120 and if you go over your max you will toss an exception. Also, you don't want someone to be able to shake the mouse endlessly to refill the bar. Filling it back to a max of 90% is sufficient, I think.
Vegeta4ss  Wednesday, April 01, 2009 6:40 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView Selection events
• Anonymous Types and the DataGridView
• Masked DataGridViewCell
• datagridview autosize headers but keep them resizable
• Changing data in DataTable does not update bound DataGridView
• ADO.NET, DataSet and pulling related data from the SQL 2k
• SetDataBinding syntax help.
• DataGridView Error. Help !
• CheckBox in Datagridview, but bounded to a Datatable.
• DataGridView new row problem