c# - 在串行端口的 DataReceived 事件处理程序中读取数据时尝试显示文本时出现死锁

标签 c# multithreading events serial-port

  • 我正在 PC 和嵌入式设备之间实现串行协议(protocol)。
  • 我正在将文件内容从 PC 分块发送到设备。
  • 在我开始发送文件之前,我会发送文件的名称和大小,然后等待来自设备的 ACK,然后开始循环发送 block 并等待每个 block 的确认。
  • 我正在使用事件将 DataReceived 线程中收到的 ACK 与发送数据 block 的主线程同步。

问题: 在我添加这一行之前,上述所有工作都很好:

textBoxLog.Invoke((MethodInvoker)(() => textBoxLog.AppendText(str)));

所以我可以向用户更新发送进度,当代码中有这一行时,主线程中未收到事件,程序处于死锁状态。

知道为什么吗?

这是代码的简短总结:

    public partial class MainForm : Form
    {
        AutoResetEvent waitHandle_1 = new AutoResetEvent(false);
        AutoResetEvent waitHandle_2 = new AutoResetEvent(false);

        public MainForm()
        {
            InitializeComponent();

            try
            {
                myPort = new System.IO.Ports.SerialPort("COM37");
                myPort.BaudRate = 460800;
                myPort.Parity = System.IO.Ports.Parity.None;
                myPort.DataBits = 8;
                myPort.StopBits = System.IO.Ports.StopBits.One;
                myPort.Handshake = System.IO.Ports.Handshake.None;
                myPort.ReadTimeout = 5000;
                myPort.DataReceived += new SerialDataReceivedEventHandler(SerialPortDataReceived);

                if (myPort.IsOpen == false) //if not open, open the port
                    myPort.Open();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

        // Serial port received event handler
        private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (myPort.BytesToRead > 0)
            {
                var count = myPort.BytesToRead;
                var bytes = new byte[count];
                myPort.Read(bytes, 0, count);
                AddResponseBytes(bytes);
            }
        }

        List<byte> Data = new List<byte>();
        private void AddResponseBytes(byte[] bytes)
        {
            // Analyzing message byte, wait for full message
            ProcessResponse(responseData, msgType);
        }

        private void ProcessResponse(List<byte> bytes, mixCommandType responseType)
        {
            String str;

            switch (responseType)
            {
                case CommandType.PC_TO_DEVICE_RESPONSE:
                    str = "Test text";
                    waitHandle_1.Set();
                    textBoxLog.Invoke((MethodInvoker)(() => textBoxLog.AppendText(str)));
                    break;
                case CommandType.CHUNK_ACK:
                    waitHandle_2.Set();
                    break;
            }
        }

        private void buttonSendFile_Click(object sender, EventArgs e)
        {
            // Open file and send message to device with file size and wait for ACK

            waitHandle_1.WaitOne();

            Console.WriteLine("Got response - Continue to send");

            uint i = 0;
            while (i < bytes.Length)
                {
                    //Send chunk of the file to the device and whait for chunk ack
                    waitHandle_2.WaitOne();
                }
        }
  }

最佳答案

那是因为 Invoke 将您传递给 UI(主)线程的任何内容分派(dispatch)(您可能已经知道),但是您的 UI 线程已经很忙,它在 buttonSendFile_Click< 中被阻塞 此处:

waitHandle_1.WaitOne();

或在这里

waitHandle_2.WaitOne();

Invoke 也是阻塞调用 - 在分派(dispatch)完成之前它不会返回 - 所以你有经典的死锁。

一般来说,阻塞 UI 线程是不好的,尤其是在等待句柄上。要解决您的问题 - 使用后台线程执行您现在在 buttonSendFile_Click 处理程序中执行的操作。

关于c# - 在串行端口的 DataReceived 事件处理程序中读取数据时尝试显示文本时出现死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37601382/

相关文章:

Java泛型事件系统未经检查的调用

c# - C#中的字符串结尾

java - 从作为后台作业运行的 shell 脚本生成进程

events - 等待akka actor系统可用

android - 长按 surfaceView ( android )

ruby - ruby 队列不是线程安全的,为什么队列不同步?

c# - 移动 UIElement 时,wpf 相对于窗口的平移点会导致偏移

c# - 向 C# 公开 ISO C++ 类

c# - 有没有办法在 where 子句中访问私有(private)属性(property)?

c# - 将Label与count查询结果绑定(bind)