SerialPort.DataReceived-线程数 [英] SerialPort.DataReceived - thread count

查看:181
本文介绍了SerialPort.DataReceived-线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

这些天SerialPort在CP上似乎很流行.我也在开发使用它的应用程序.

通过DataReceived事件接收来自外部设备的数据. MSDN [ ^ ]告诉我们事件处理程序在辅助线程上运行.

这是否意味着每个事件都会启动一个新的事件处理线程,或者是否恰好有一个事件处理线程(与UI线程分离)可以一个接一个地处理DataReceived事件?

换句话说,是否存在同样的问题:是否存在持久的DataReceived事件处理例程导致SerialPort丢失数据的风险?

Hi experts,

the SerialPort seems to be quite popular these days on CP. I''m developing an application that uses it, too.

Data from an external device is received via the DataReceived event. MSDN[^] tells us that the event handler runs on a secondary thread.

Does that mean that each event starts a new event handling thread or is there exactly one event handling thread (separated from the UI thread) that handles the DataReceived events one after another?

Same question in other words: Is there a risk that a long lasting DataReceived event handling routine causes SerialPort to miss data?

推荐答案

串行端口被缓冲,无论是在硬件中还是在Windows中,除非您在处理程序中花了很长的时间,否则几乎没有溢出的风险-请记住,事件处理程序不是中断,因此硬件在使用它时可以继续愉快地完美缓冲数据.

话虽如此,您仍然应该使事件处理程序的一般规则尽可能短,并且在使用串行端口时也不会改变.
Serial Ports are buffered, both in the hardware and within Windows so there is little risk of overrun, unless you are taking a stupidly long time in the handler - remember that event handlers are not interrupts, so the hardware can continue buffering data perfectly happily while you work with it.

Having said that, you should still keep event handlers as short as possible as a general rule, and that doesn''t change when you use a serial port.


这篇关于SerialPort.DataReceived-线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆