Question : AutoClose Timer

I have a VB.Net Windows app with a Class that is used as an inactivity timer and a form with just a button. I am trying to get the form to minimize on idle time. I am new to this type of programming. I found the inactivity class code online and really liked and was hoping to use it. I think the problem might be related to threading but I am not sure. I would really appreciate any help anyone can give me. I have attached the code.

Thanks much,
Pepper
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
'***The Form
Imports WindowsApplication1.NSAutoLogout
Public Class Form1
 
    'Start the inactivity thread
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InActivityTimer.WatchControl(Me)
    End Sub
 
    'activate the inactivate timing
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        InActivityTimer.blActivity = True
    End Sub
 
    'stop the inactivity timimg and minimize form
    Public Sub InActivityTimerDone()
        InActivityTimer.blActivity = False
        Me.WindowState = FormWindowState.Minimized
    End Sub
End Class
 
'***The Class
Imports System
Imports System.Windows.Forms
Imports System.Threading
Imports WindowsApplication1.Form1
 
Namespace NSAutoLogout
 
    Public Class InActivityTimer
        ' This is the maximum number of minutes the application can remain 
        ' without activity before the AutoLogout routine will close the 
        ' application. The field is public so that it can be changed. 
        Public Shared maxNumberSecondsIdle As Integer = 10
 
        ' Keeps the timestamp for the last time activity was detected. 
        Private Shared dtLastActivity As Date = DateTime.Now
 
        ' To determine if we want the control checked for inactivity or not. 
        Public Shared blActivity As Boolean
 
        ' This is the method that will serve as the background thread. 
        Private Shared Sub CheckForExceededIdleTime()
            ' Sets up an infinite loop 
            While True
                ' If the last time activity occured + the Maximum Allowable 
                ' Idle time is less than the current time, then the system 
                ' should be shut down 
                If dtLastActivity.AddSeconds(maxNumberSecondsIdle) < DateTime.Now Then
                    Console.WriteLine("Inactivity Exceeded")
 
                    ' modify this for whatever it is you want to do when the 
                    ' inactivity time is exceeded
                    If blActivity Then Form1.InActivityTimerDone()
 
                Else
                    Console.WriteLine("Not AutoLogged Out")
                End If
                ' Probably don't need this running every second 
                ' Perhaps every minute would be better in a production system 
                Thread.Sleep(1000)
            End While
        End Sub
 
        ' Static Constructor. Used to launch the background thread 
        Shared Sub New()
            Dim ts As New ThreadStart(AddressOf CheckForExceededIdleTime)
            Dim t As New Thread(ts)
            Dim f1 As New Form1
            ' Ensures background thread is killed when 
            ' last foreground terminates 
            t.IsBackground = True
 
            ' Don't want this thread taking up too much CPU 
            t.Priority = ThreadPriority.BelowNormal
            t.Start()
        End Sub
 
        ' Private Constructor Prevents Instantiation 
        Private Sub New()
        End Sub
 
        ' This is the method called to watch a form for activity
        Public Shared Sub WatchControl(ByVal c As Control)
            ' If the control is a textbox then we want to watch 
            ' for KeyStrokes since these signify activity. 
            If TypeOf c Is TextBox Then
                Dim t As TextBox = DirectCast(c, TextBox)
                AddHandler t.KeyPress, AddressOf MethodWhichResetsTheDateofLastActivity
                ' Register an event listener for the keypress event 
 
                ' Register an event listener for the MouseMove event 
                AddHandler c.MouseMove, AddressOf MethodWhichResetsTheDateofLastActivity
            Else
                ' If the control is not a TextBox then the we want to watch 
                ' for MouseMovement since this signifies activity 
                AddHandler c.MouseMove, AddressOf MethodWhichResetsTheDateofLastActivity
            End If
            ' Checks to see if the control is a container for other controls 
            ' if so then we need to watch of all the constituent controls/ 
            If c.Controls.Count > 0 Then
                For Each cx As Control In c.Controls
                    ' Recursive call 
                    WatchControl(cx)
                Next
            End If
        End Sub
 
        ' This method resets the datetime stamp which indicates when the last 
        ' activity occured. Overloaded to support KeyPressEventArgs parameter 
        Private Shared Sub MethodWhichResetsTheDateofLastActivity(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Console.WriteLine("Keyboard Activity Detected")
            dtLastActivity = DateTime.Now
        End Sub
 
        ' This method resets the datetime stamp which indicates when the last 
        ' activity occured. Overloaded to support MouseEventArgs parameter 
        Private Shared Sub MethodWhichResetsTheDateofLastActivity(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Console.WriteLine("Mouse Activity Detected")
            dtLastActivity = DateTime.Now
        End Sub
    End Class
End Namespace

Answer : AutoClose Timer

Here is a different approach that you might like...

See IMessageFilter:
http://msdn.microsoft.com/en-us/library/system.windows.forms.imessagefilter(VS.71).aspx

I noticed that WM_MOUSEMOVE was firing periodically even though the mouse was not being moved so I removed it from the criteria.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
Public Class Form1
 
    Private WithEvents mf As New MyFilter
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Application.AddMessageFilter(mf)
    End Sub
 
    Private Sub mf_Inactivity() Handles mf.Inactivity
        Me.WindowState = FormWindowState.Minimized
    End Sub
 
    Private Class MyFilter
        Implements IMessageFilter
 
        Public Event Inactivity()
 
        Private dtTarget As DateTime
 
        ' Private Const WM_MOUSEMOVE As Integer = &H200 ' UNRELIABLE INDICATOR
        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 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 WithEvents tmr As New Timer
 
        Public Sub New()
            tmr.Interval = 1000
            tmr.Start()
        End Sub
 
        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
            Select Case m.Msg
                Case WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP, _
                    WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, _
                    WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK
 
                    dtTarget = DateTime.Now.AddSeconds(10)
                    tmr.Start()
            End Select
        End Function
 
        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            If DateTime.Now > dtTarget Then
                tmr.Stop()
                RaiseEvent Inactivity()
            End If
        End Sub
 
    End Class
 
End Class
Random Solutions  
 
programming4us programming4us