mardi 24 février 2015

Cross-thread operation not valid: Control 'xxx' accessed from a thread other than the thread it was created on

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:


From the Form Load



Application.AddMessageFilter(Me)
timerTest= New System.Timers.Timer()
AddHandler timerTest.Elapsed, AddressOf OnTimedTestEvent
timerTest.Enabled = True


Implementing IMessageFilter



Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage

If (m.Msg >= &H100 And m.Msg <= &H109) Or (m.Msg >= &H200 And m.Msg <= &H20E) Then
timerTest.Stop()
timerTest.Interval = 30000
timerTest.Start()
End If
End If

End Function


Event trigger



Private Sub OnTimedTestEvent(source As Object, e As ElapsedEventArgs)

timerTest.Stop()
HideForm()

End Sub


Hide the form



Private Sub HideForm()

Me.Visible = False <--- getting error here
End Sub

Aucun commentaire:

Enregistrer un commentaire