title: Cross-thread operation error using a Timer class
body: <p>I am having the above error showing in my code. When I was using the windows form's timer, I was able to run my application successfully. Now that I have changed to system.timers, I dont know what I'm doing wrong. I am implemeting IMessageFilter to listen to mouse/keyboard movement and restarting the timer if there is interaction. If there is no interaction, hide the form. Please, can someone help me out? I am using VB.Net and here is the code I am using:</p>
  
  <p><strong>From the Form Load</strong></p>
  
  <pre><code>Application.AddMessageFilter(Me)
  timerTest= New System.Timers.Timer()
  AddHandler timerTest.Elapsed, AddressOf OnTimedTestEvent
  timerTest.Enabled = True
  </code></pre>
  
  <p><strong>Implementing IMessageFilter</strong></p>
  
  <pre><code>Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
  
              If (m.Msg &gt;= &amp;H100 And m.Msg &lt;= &amp;H109) Or (m.Msg &gt;= &amp;H200 And m.Msg &lt;= &amp;H20E) Then
                  timerTest.Stop()
                  timerTest.Interval = 30000
                  timerTest.Start()
                  End If
              End If
  
      End Function
  </code></pre>
  
  <p><strong>Event trigger</strong></p>
  
  <pre><code>Private Sub OnTimedTestEvent(source As Object, e As ElapsedEventArgs)
  
          timerTest.Stop()
          HideForm()
  
      End Sub
  </code></pre>
  
  <p><strong>Hide the form</strong></p>
  
  <pre><code>Private Sub HideForm()
  
              Me.Visible = False &lt;--- getting error here
  End Sub
  </code></pre>