1

I'm developing an application that use an external device to communicate with an electronic board using USB protocol. When I want to display results obtained during communication inside TextBlock an error msg appears saying

The calling thread cannot access this object because a different thread owns it

How could I stop this thread or print the appropriate info in my textblock?

Here the code used :

    /// ******************************************************************
    /// <summary>
    /// RX_Thread
    /// 
    /// Description: 
    /// Second thread which receives data from configured receive port
    /// (CANDemo_RxChannel)of XL interface.
    /// </summary>
    /// ******************************************************************
    #region EVENT THREAD (RX)
    public  void RX_Thread()
    {
        // object to be fill with received data
        XLClass.xl_event receivedEvent = new XLClass.xl_event();

        // result of XL driver func. calls
        XLClass.XLstatus xlStatus = XLClass.XLstatus.XL_SUCCESS;

        // WaitForSingleObject values
        WaitResults waitResult = new WaitResults();


        // note: this thread is destroyed by MAIN
        while (true)
        {
            // wait for hardware events
            waitResult = (WaitResults)WaitForSingleObject(CANDemo_RxChannel.eventHandle, 1000);

            // if event occured... 
            if (waitResult != WaitResults.WAIT_TIMEOUT)
            {
                // ...init xlStatus first
                xlStatus = XLClass.XLstatus.XL_SUCCESS;

                // afterwards: while hw queue is not empty...
                while (xlStatus != XLClass.XLstatus.XL_ERR_QUEUE_IS_EMPTY)
                {
                    // ...receivedata from hardware queue
                    xlStatus = CANDemo_RxChannel.xlReceive(ref receivedEvent);

                    //  if receiveing succeed...
                    if (xlStatus == XLClass.XLstatus.XL_SUCCESS)
                    {
                        // ...print rx data
                        string BigContained = CANDemo.XL_GetEventString(receivedEvent);
                        //GetInfo(BigContained);
                       // ((XLClass.xl_event)xlEventCollection.xlEvent[0]).tagData.can_Msg.id = 0x74D;
                        if (receivedEvent.tagData.can_Msg.id == 0x76D)
                        {
                            if (receivedEvent.tagData.can_Msg.data[0] == Convert.ToByte(2) && receivedEvent.tagData.can_Msg.data[1] == Convert.ToByte(80) && receivedEvent.tagData.can_Msg.data[2] == Convert.ToByte(250))
                            {
// i want to writ the data in my Texbloxk but i get error :
                                    txtframeContainer.Text = "Enter in diagnostic mode   SupplierSpecific --> Ok ";
                            }
                        }
                    }
                }
            }
            // no event occured
        }
    }
    #endregion
    /// ------------------------------------------------------------------

waiting for your feedback guys...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2933082
  • 263
  • 3
  • 10
  • here some additional data of error obtained : – user2933082 Nov 12 '13 at 10:39
  • at System.Windows.Threading.Dispatcher.VerifyAccess() at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at System.Windows.Controls.ContentControl.set_Content(Object value) at RelevantInfo.MainWindow.RX_Thread() in C:\Users\barras\Desktop\who knows\RelevantInfo\RelevantInfo\MainWindow.xaml.cs:line 219 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, – user2933082 Nov 12 '13 at 10:39
  • Please _edit_ your question when you want to add information. – Gusdor Nov 12 '13 at 10:40
  • Also, your question title has very little to do with your problem. – Gusdor Nov 12 '13 at 10:40

2 Answers2

1

You can only update the ui from your ui thread, so if you update a ui object you have to get the ui thread and invoke the update on that thread:

if (Application.Current.Dispatcher.CheckAccess()) 
{ 
     txtframeContainer.Text = Result; 
} 
else 
{ 
    Application.Current.Dispatcher.Invoke((Action)(() =>
    {
        txtframeContainer.Text = "Enter in diagnostic mode   SupplierSpecific --> Ok ";
    }));
}
Peter
  • 27,590
  • 8
  • 64
  • 84
  • thank you thats work as magic here what i have used : private void YourMethod(string Result) { if (Application.Current.Dispatcher.CheckAccess()) { textBox1.Text = Result; } else { access Application.Current.Dispatcher.Invoke(new System.Action(() => YourMethod(Result))); } } – user2933082 Nov 12 '13 at 10:46
  • yes thats it , thanks man ;) – user2933082 Nov 12 '13 at 10:52
0

This error will be caused by you trying to update a UI control on a thread that is not the UI thread.

Have a look at this question: How to update the GUI from another thread in C#? it should give you the answer

Community
  • 1
  • 1
Mike Norgate
  • 2,393
  • 3
  • 24
  • 45