这里只是一个小问题,这个代码我成功地从我的串行端口获取数据,但是当我想要在标签中看到这些数据时,我得到了这个: System.InvalidOperationException: '调用线程不能访问这个对象,因为另一个线程是所有者'。 我不确定什么是调度程序以及如何使用它。你能解释给我吗?使用串行端口的线程问题
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
//init serialport comport
SerialPort comport = (SerialPort)sender;
// Shortened and error checking removed for brevity...
if (!comport.IsOpen) return;
int bytes = comport.BytesToRead;
byte[] buffer = new byte[bytes];
comport.Read(buffer, 0, bytes);
HandleSerialData(buffer, comport);
}
public void HandleSerialData(byte[] respBuffer, SerialPort comport)
{
StringBuilder hex = new StringBuilder(respBuffer.Length * 2);
foreach (byte b in respBuffer)
hex.AppendFormat("{0:x2}", b);
string hex2 = hex.ToString();
hex2 = hex2.Substring(22, 8);
EnOcean_Label.Dispatcher.CheckAccess();
EnOcean_Label.Content = hex2;
}**
'Dispatcher.BeginInvoke((=){EnOcean_Label.Content = hex2;});'将执行添加到原始线程的调用堆栈。 –
你使用wpf还是表单? –
对于多线程应用程序,UI在另一个线程上执行。您必须通过调度程序(或类似的)调用某些内容来更新UI。 – BurnsBA