首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与外部模块通信时线程未停止

与外部模块通信时线程未停止
EN

Stack Overflow用户
提问于 2013-11-12 10:36:19
回答 2查看 161关注 0票数 1

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

调用线程无法访问此对象,因为其他线程拥有该对象。

我如何停止这个线程或打印适当的信息在我的文本块?

这里所用的代码是:

代码语言:javascript
复制
    /// ******************************************************************
    /// <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
    /// ------------------------------------------------------------------

等待你们的反馈..。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-12 10:39:37

您只能从ui线程更新ui,所以如果更新ui对象,就必须获得ui线程并调用该线程上的更新:

代码语言:javascript
复制
if (Application.Current.Dispatcher.CheckAccess()) 
{ 
     txtframeContainer.Text = Result; 
} 
else 
{ 
    Application.Current.Dispatcher.Invoke((Action)(() =>
    {
        txtframeContainer.Text = "Enter in diagnostic mode   SupplierSpecific --> Ok ";
    }));
}
票数 1
EN

Stack Overflow用户

发布于 2013-11-12 10:39:16

此错误将由您试图更新不是UI线程的线程上的UI控件造成的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19926817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档