Results 1 to 5 of 5

Thread: [Solved] Threading error

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    3

    [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    3

    Re: [Need Help] Threading error

    To jmcilhinney: Yes, I am using WPF.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Class Window1
    2.  
    3.     Private WithEvents clock As New Timers.Timer With {.Interval = 5000, .Enabled = True, .AutoReset = False}
    4.  
    5.     Private Sub clock_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles clock.Elapsed
    6.         Me.SetLabelContent("Hello World")
    7.     End Sub
    8.  
    9.     Private Sub SetLabelContent(ByVal content As Object)
    10.         If Me.Label1.Dispatcher.Thread Is System.Threading.Thread.CurrentThread Then
    11.             Me.Label1.Content = content
    12.         Else
    13.             Me.Label1.Dispatcher.Invoke(New Action(Of Object)(AddressOf SetLabelContent), content)
    14.         End If
    15.     End Sub
    16.  
    17. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    3

    Re: [Need Help] Threading error

    Problem solved.
    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
  •  



Click Here to Expand Forum to Full Width