The calling thread cannot access this object...

Supporting Visual Studio on Windows
jamie.dixon
Fresh meat
Posts: 2
Joined: Wed Aug 17, 2011 6:35 am
Contact:

The calling thread cannot access this object...

Postby jamie.dixon » Wed Aug 17, 2011 6:38 am

I hooked up a 8/8/8 Interface kit with 2 pressure sensors to it. Using VS2010, I created a WPF application like so:

Code: Select all

        InterfaceKit _interfaceKit;

        public MainWindow()
        {
            InitializeComponent();
            _interfaceKit = new InterfaceKit();
            _interfaceKit.open();
            _interfaceKit.waitForAttachment(3000);
            _interfaceKit.SensorChange += new SensorChangeEventHandler(_interfaceKit_SensorChange);

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(String.Format("Name={0}",_interfaceKit.Name));
            stringBuilder.Append(Environment.NewLine);
            stringBuilder.Append(String.Format("Label={0}", _interfaceKit.Label));

            this.InterfaceKitInfoLabel.Content = stringBuilder.ToString();

        }

        void _interfaceKit_SensorChange(object sender, SensorChangeEventArgs e)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(String.Format("Index={0}", e.Index));
            stringBuilder.Append(Environment.NewLine);
            stringBuilder.Append(String.Format("Value={0}", e.Value));

            this.PressureSensor01Label.Content = stringBuilder.ToString();
        }


I am getting the following error:
The calling thread cannot access this object because a different thread owns it.

There is nothing in the documentation on Thread Affinity. Any ideas what I need to do differently?

User avatar
Patrick
Lead Developer
Posts: 3158
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: The calling thread cannot access this object...

Postby Patrick » Wed Aug 17, 2011 9:21 am

The sensor change event comes in on a separate thread, so you'll probably have to invoke on your label to set it from the event.

This is generally dealt with in the library but I'm guessing something in WPF messes with that so you'll have to do it manually.

-Patrick


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 2 guests