13 years C/C++/C# programing, focus on embedded and mobile device development.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  127 随笔 :: 1 文章 :: 144 评论 :: 29万 阅读
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

http://blogs.msdn.com/bclteam/archive/2006/10/10/Top-5-SerialPort-Tips-_5B00_Kim-Hamilton_5D00_.aspx

Tip 3.
Take care to avoid deadlock when calling Close on the SerialPort in response to a GUI event.

Examples of bugs this tip addresses
  • An app involving the UI and the SerialPort freezes up when closing the SerialPort
  • Deadlock can occur if Control.Invoke() is used in serial port event handlers

This is probably the most frustrating SerialPort issue because it may happen so rarely that it’s hard to diagnose. Deadlocks can happen if you have an event handler trying to work with a GUI control while the GUI thread is trying to close the SerialPort. This happens in the following example.

public partial class Form1 : Form
{
        SerialPort m_port;
 
        public Form1()
        {
            InitializeComponent();
            m_port = new SerialPort("COM2", 115200);
            m_port.DataReceived += new
                 SerialDataReceivedEventHandler(m_port_DataReceived);
            m_port.Open();
        }
 
        void m_port_DataReceived(object sender,
                 SerialDataReceivedEventArgs e)
        {
            this.Invoke(new EventHandler(delegate
            {
                m_port.Close();
            }));
        }
}

This example looks a bit strange, but it forces the deadlock to occur. The typical scenario we encounter is occasional deadlock in an app that has a data received handler trying to update the GUI at the same time the GUI thread is trying to close the SerialPort (for example, in response to the user clicking a Close button).

The reason deadlock happens is that Close() waits for events to finish executing before it closes the port. You can address this problem in your apps in two ways:

  • In your event handlers, replace every Control.Invoke call with Control.BeginInvoke, which executes asynchronously and avoids the deadlock condition. This is commonly used for deadlock avoidance when working with GUIs.
  • Call serialPort.Close() on a separate thread. You may prefer this because this is less invasive than updating your Invoke calls.
posted on   woaiusd  阅读(504)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
点击右上角即可分享
微信分享提示