1

我正在开发一个应用程序,它使用外部设备与使用 USB 协议的电子板进行通信。当我想显示在 TextBlock 内部通信期间获得的结果时,会出现错误消息说

调用线程无法访问此对象,因为不同的线程拥有它

我怎样才能停止这个线程或在我的文本块中打印适当的信息?

这里使用的代码:

    /// ******************************************************************
    /// <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
    /// ------------------------------------------------------------------

等待您的反馈,伙计们...

4

2 回答 2

1

您只能从您的 ui 线程更新 ui,因此如果您更新一个 ui 对象,您必须获取 ui 线程并在该线程上调用更新:

if (Application.Current.Dispatcher.CheckAccess()) 
{ 
     txtframeContainer.Text = Result; 
} 
else 
{ 
    Application.Current.Dispatcher.Invoke((Action)(() =>
    {
        txtframeContainer.Text = "Enter in diagnostic mode   SupplierSpecific --> Ok ";
    }));
}
于 2013-11-12T10:39:37.470 回答
0

此错误将由您尝试在不是 UI 线程的线程上更新 UI 控件引起。

看看这个问题:How to update the GUI from another thread in C#? 它应该会给你答案

于 2013-11-12T10:39:16.377 回答