|
-
Jan 19th, 2010, 08:37 PM
#1
Thread Starter
New Member
[Solved] Threading error
Hi, I am using phidget device to collect information from the sensor. However, whenever I run the program, this error shows up, saying that "The calling thread cannot access the object because a different thread owns it"
The code:
Private Sub phid_SensorChange(ByVal sender As Object, ByVal e As Phidgets.Events.SensorChangeEventArgs) Handles phid.SensorChange
If e.Index = 0 Then
lblDisplayTemp.Content = e.Value.ToString
End If
End Sub
How do I solve this problem?
Last edited by griggle; Jan 28th, 2010 at 03:29 AM.
-
Jan 19th, 2010, 09:27 PM
#2
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.
-
Jan 19th, 2010, 09:48 PM
#3
Thread Starter
New Member
Re: [Need Help] Threading error
To jmcilhinney: Yes, I am using WPF.
-
Jan 20th, 2010, 12:47 AM
#4
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.
-
Jan 27th, 2010, 08:44 PM
#5
Thread Starter
New Member
Re: [Need Help] Threading error
Last edited by griggle; Jan 28th, 2010 at 03:30 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|