Is it necessary to synchronize .NET SerialPort writes/reads?(是否有必要同步 .NET SerialPort 写入/读取?)
问题描述
在我的应用程序中,我使用 .NET SerialPort 类来读取和写入数据.读取是使用 DataReceived 事件完成的,我假设在 ThreadPool 线程内部.编写由 UI 线程(WinForms)完成.
In my application I use the .NET SerialPort class for reading and writing data. The reading is done using the DataReceived event, I assume internally on a ThreadPool thread. The writing is done by the UI thread (WinForms).
我想知道,是否有必要同步对 SerialPort 实例的访问,以便不会同时发生读/写.我的良心告诉我应该对这些调用加锁,但我很困惑,因为我在 Internet 上找到的所有 C# SerialPort 示例根本不使用锁定.
I was wondering, if it's necessary to synchronize the access to the SerialPort instance, so that no Reads/Writes can happen at the same time. My conscience tells me I should put locks around these calls, but I'm puzzled as all C# SerialPort examples I find on the Internet don't use locking at all.
推荐答案
这里有一个很棒的主题,SerialPort 类的作者参与:
Here's a great thread on the topic, with the author of the SerialPort class participating:
MSDN:如何SerialPort 是否处理 DataReceived?
根据我的经验,我编写了十几个串行通信应用程序用作硬件模拟器,我不锁定.我当时不知道我是否安全,但在实践中,我还没有出错.(20 多位测试人员和自动化测试机器几乎持续使用一年)也就是说,我的应用程序不会离开公司,如果我正在编写供公众使用的应用程序,我可能会更加小心.
From my experience, I've written a dozen serial communication apps for use as hardware simulators, I don't lock. I didn't know at the time if I was safe or not, but in practice, I haven't had an error yet. (a year of near constant use by 20+ testers and automated test machines) That said, my applications don't leave the company, if I were writing apps for public consumption I might take more care.
这篇关于是否有必要同步 .NET SerialPort 写入/读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否有必要同步 .NET SerialPort 写入/读取?
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01