我正在开发一个应用程序,它使用外部设备与使用 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
/// ------------------------------------------------------------------
等待您的反馈,伙计们...