c#

C# DataReceived事件的跨线程通信

小樊
87
2024-08-08 21:05:41
栏目: 编程语言

在C#中,可以通过使用委托和事件来实现跨线程通信。具体来说,可以在主线程中订阅DataReceived事件,并在事件处理程序中使用Invoke方法将事件委托到UI线程上执行。这样可以确保在事件处理程序中更新UI控件而不会引发线程访问异常。

下面是一个示例代码,演示了如何在C#中实现DataReceived事件的跨线程通信:

using System;
using System.IO.Ports;
using System.Windows.Forms;

public class SerialPortManager
{
    private SerialPort serialPort;

    public SerialPortManager(string portName)
    {
        serialPort = new SerialPort(portName);
        serialPort.DataReceived += SerialPortDataReceived;
    }

    public void Open()
    {
        serialPort.Open();
    }

    public void Close()
    {
        serialPort.Close();
    }

    private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string data = serialPort.ReadExisting();

        // 使用Invoke方法将事件委托到UI线程上执行
        Form1 form = Application.OpenForms[0] as Form1;
        form.Invoke(new Action(() =>
        {
            // 在UI线程上更新UI控件
            form.textBox1.Text = data;
        }));
    }
}

public class Form1 : Form
{
    private SerialPortManager serialPortManager;

    public Form1()
    {
        serialPortManager = new SerialPortManager("COM1");
        serialPortManager.Open();
    }
}

在上面的示例中,SerialPortManager类用于管理串口通信,并在DataReceived事件中更新UI控件。在Form1类的构造函数中,订阅了DataReceived事件,并在事件处理程序中使用Invoke方法将更新UI控件的操作委托到UI线程上执行。

通过这种方式,可以确保在串口通信中更新UI控件时不会引发线程访问异常,实现了跨线程通信。

0
看了该问题的人还看了