Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
gblnSessionTimedOut = True
Timer1.Stop()
frmLogin.ShowDialog(Me)
Catch ex As Exception
EH.ErrorMessage = "frmMain/Timer1_Tick() - " & ex.Message & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
End Sub
Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'Debug.Print("frmMain_KeyDown")
Try
Timer1.Start()
If gblnLoggedIn Then
Dim mdiFrm As Form = Me.ActiveMdiChild
Dim strForm As String = String.Empty
Dim iFound As Integer = 0
EH.ErrorMessage = String.Empty
If Not IsNothing(mdiFrm) Then 'No Form is being displayed at the moment
strForm = mdiFrm.Name
End If
If gblnF9Key Then
sbr.Items(1).Text = gFKeys2
Else
sbr.Items(1).Text = gFKeys
End If
Me.Cursor = Cursors.WaitCursor
InitializeErrorClass(EH)
Select Case e.KeyCode
Case CType(27, Keys) 'Escape Key
For Each frm As Form In Me.MdiChildren
frm.Close()
Next
stcFormSize.Width = 0
stcFormSize.Height = 0
sbr.Items(1).Text = gFKeys
Case CType(112, Keys) 'F1 Key was pressed - Login
frmLogin.ShowDialog()
Case CType(113, Keys) 'F2 Key was pressed - Log out
If gblnChangesMade Then
Dim strWarning As String = "You have unsaved changes! If you continue, all changes will be aborted." & vbCrLf & "Continue?"
Dim answer As Integer = MessageBox.Show(strWarning, "User Notification", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If answer = vbNo Then
Me.Cursor = Cursors.Default
GoTo ProcessMessage
End If
End If
gblnLoggedIn = False
Me.Cursor = Cursors.Default
gUser = String.Empty
gUser_ID = 0
Me.sbr.Items(2).Text = "Network ID: "
frmLogin.ShowDialog()
Case CType(114, Keys) 'F3 Key was pressed - Exit Program
Me.Close()
End
Case CType(115, Keys) 'F4 Key was pressed - Custom Settings
frmCustomSettings.MdiParent = Me
frmCustomSettings.Show()
frmCustomSettings.BringToFront()
Case CType(116, Keys) 'F5 Key was pressed - Certificates
CheckFunctionalityPermission(CStr(CertificatesToolStripMenuItem.Tag), gUser_ID, EH)
If EH.ErrorMessage = String.Empty Then
If CBool(gblnViewPermission) Or gblnSuperUser Then
frmCertificates.MdiParent = Me
frmCertificates.Show()
frmCertificates.BringToFront()
End If
Else
GoTo ProcessMessage
End If
Case CType(117, Keys) 'F6 Key was pressed - User Setup
CheckFunctionalityPermission(CStr(LookupTablesToolStripMenuItem.Tag), gUser_ID, EH)
If EH.ErrorMessage = String.Empty Then
If CBool(gblnViewPermission) Or gblnSuperUser Then
frmUserSetup.MdiParent = Me
frmUserSetup.Show()
frmUserSetup.BringToFront()
End If
Else
GoTo ProcessMessage
End If
Case CType(118, Keys) 'F7 Key was pressed - Lookup Tables
'AddHandler frmLookups.EnableDataRefresh, AddressOf frmCalibration_3.EnableDataRefreshButton
CheckFunctionalityPermission(CStr(LookupTablesToolStripMenuItem.Tag), gUser_ID, EH)
If EH.ErrorMessage = String.Empty Then
If CBool(gblnViewPermission) Or gblnSuperUser Then
frmLookups.MdiParent = Me
frmLookups.Show()
frmLookups.BringToFront()
End If
Else
GoTo ProcessMessage
End If
Case CType(119, Keys) 'F8 Key was pressed - Equipment Verify
CheckFunctionalityPermission(CStr(EquipmentVerificationToolStripMenuItem.Tag), gUser_ID, EH)
If EH.ErrorMessage = String.Empty Then
If CBool(gblnViewPermission) Or gblnSuperUser Then
frmEquipVerify.MdiParent = Me
frmEquipVerify.Show()
frmEquipVerify.BringToFront()
End If
Else
GoTo ProcessMessage
End If
Case CType(121, Keys) 'F10 Key was pressed - Calibration Houses
CheckFunctionalityPermission(CStr(EquipmentVerificationToolStripMenuItem.Tag), gUser_ID, EH)
If EH.ErrorMessage = String.Empty Then
If CBool(gblnViewPermission) Or gblnSuperUser Then
frmCalibrationHouse.MdiParent = Me
frmCalibrationHouse.Show()
frmCalibrationHouse.BringToFront()
End If
Else
GoTo ProcessMessage
End If
Case CType(122, Keys) 'F11 Key was pressed - Sensor Calibration
rcMessage.Visible = True
ShowLoadStatusMessage("Loading Form...", True)
If EH.ErrorMessage > String.Empty Then
GoTo ProcessMessage
End If
CheckFunctionalityPermission(CStr(SensorCalibrationToolStripMenuItem.Tag), gUser_ID, EH)
If EH.ErrorMessage = String.Empty Then
Application.DoEvents()
If CBool(gblnViewPermission) Or gblnSuperUser Then
frmCalibration_3.MdiParent = Me
frmCalibration_3.Show()
frmCalibration_3.BringToFront()
End If
Else
GoTo ProcessMessage
End If
ShowLoadStatusMessage("", False)
Case CType(123, Keys)
'F12 Key is unassigned
End Select
Else
Select Case e.KeyCode
Case CType(112, Keys) 'F1 Key was pressed - Login
frmLogin.ShowDialog()
If gblnLoggedIn Then
SetNavigationPermissions(MenuStrip1, ts1)
If EH.ErrorMessage > String.Empty Then
GoTo ProcessMessage
End If
End If
End Select
End If
Me.Cursor = Cursors.Default
ProcessMessage:
Catch ex As Exception
EH.ErrorMessage = "frmMain/frmMain_KeyDown() - " & ex.Message & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Timer1.Stop()
gblnSessionTimedOut = True
frmLogin.ShowDialog(Me)
Catch ex As Exception
EH.ErrorMessage = "frmMain/Timer1_Tick() - " & ex.Message & "~E"
End Try
EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
End Sub
Public Class Form1
Implements IMessageFilter
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_LBUTTONDBLCLK As Integer = &H203
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const WM_RBUTTONUP As Integer = &H205
Private Const WM_RBUTTONDBLCLK As Integer = &H206
Private Const WM_MBUTTONDOWN As Integer = &H207
Private Const WM_MBUTTONUP As Integer = &H208
Private Const WM_MBUTTONDBLCLK As Integer = &H209
Private Const WM_MOUSEWHEEL As Integer = &H20A
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYDOWN As Integer = &H104
Private Const WM_SYSKEYUP As Integer = &H105
Private TargetDateTime As DateTime
Private IdleTimeInMinutes As Integer = 10
Private WithEvents tmr As New System.Windows.Forms.Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TargetDateTime = DateTime.Now.AddMinutes(IdleTimeInMinutes)
tmr.Interval = 1000
tmr.Start()
Application.AddMessageFilter(Me)
End Sub
Private Sub tmr_Tick(sender As Object, e As EventArgs) Handles tmr.Tick
Dim TS As TimeSpan = TargetDateTime.Subtract(DateTime.Now)
If TS.TotalMilliseconds > 0 Then
' optional: show much time to the timeout trigger somewhere
Me.Text = TS.ToString("hh\:mm\:ss")
Else
' idle timeout reached
tmr.Stop()
' do something:
Application.Exit()
End If
End Sub
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
Select Case m.Msg
Case WM_MOUSEWHEEL, WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP, _
WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, _
WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK, _
WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MBUTTONDBLCLK
TargetDateTime = DateTime.Now.AddMinutes(IdleTimeInMinutes)
End Select
Return False
End Function
End Class
Instead of saying that something is stupid, try to understand it. The Timer control is one of the useful tools there is to solve a lot of problems, yours included.
And the Timer is very simple to use. Simply set it's Interval property to the time you want, in milliseconds and then start the timer (by calling its Start method). If the Timer is not stopped before the specified time is reached (by calling its Stop method), it will fire it's Tick event. If you call Start again before the time as elapsed, it resets and start counting again for the specified time. That is all.
You do not specify in you question what is the condition that will require the application to be locked. Since you talk about the Timer, I suppose that you want to lock the application if it is idle for some time, but it is not clear. Could you be more specific?
If this is the case, then the solution is relatively easy if your application has only one form, but might be more complex if you work will multiple forms. One also has to define what "idle" means, because you have to detect that situation, and nothing that I know is built-in to do that for your.
This basically means that you have to start a timer for the desired period, detect all the keypresses, mouse clicks and long processes, and restart the Timer on each one. As long as you restart the Timer, it will not expire and its Tick event will never fire.
But if the user stops working and the application itself does not do some processing in the background, the Timer will not be restarted and will end up firing it's Tick event after the specified Interval. From there you can display the necessary messages and forms to lock the application and force the user to log back in.