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.