Re: [Need Help] Threading error
You cannot access most control members directly from a thread other than the one on which its handle was created. In most apps the "main thread" or "UI thread" is the one on which your app was started and the one that creates all your forms and, therefore, all your controls. As such, you can only access most control properties on that main thread.
Before I go any further, I see that you are accessing the Content property of what appears to be a Label. That would suggest that you're using WPF rather than WinForms. Is that the case? If so then this site has a forum dedicated to WPF that be a more appropriate place for this thread. Let us know if it is WPF and we can get the mods to move the thread. The way to handle this situation is most likely different in WPF than WinForms.
Re: [Need Help] Threading error
To jmcilhinney: Yes, I am using WPF.
Re: [Need Help] Threading error
Just had a quick search:
http://www.google.com.au/search?q=cr...ient=firefox-a
http://anoriginalidea.wordpress.com/...dating-in-wpf/
Based on my knowledge of this situation in WinForms and that information, I just tried this and it worked:
vb.net Code:
Class Window1
Private WithEvents clock As New Timers.Timer With {.Interval = 5000, .Enabled = True, .AutoReset = False}
Private Sub clock_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles clock.Elapsed
Me.SetLabelContent("Hello World")
End Sub
Private Sub SetLabelContent(ByVal content As Object)
If Me.Label1.Dispatcher.Thread Is System.Threading.Thread.CurrentThread Then
Me.Label1.Content = content
Else
Me.Label1.Dispatcher.Invoke(New Action(Of Object)(AddressOf SetLabelContent), content)
End If
End Sub
End Class
You can follow the CodeBank link in my signature and check out my Accessing Controls From Worker Threads post to see how to create code like this. As you should be able to see, the difference is minor between WinForms and WPF.
Re: [Need Help] Threading error