在 C# 中,SerialPort class 的 DataReceived 事件处理程序是否在它自己的线程上执行?

In C#, does the DataReceived event handler of the SerialPort class execute on it's own thread?

我有一个 C# 程序,它使用 System.IO.Ports.SerialPort class 通过串行端口进行通信。为了接收数据,我订阅了它的 DataReceived 事件处理程序。

我的问题是,当接收到数据时 - DataReceived 事件 运行 是在它自己的线程中,还是保持同步?也就是说,在 DataReceived 完成之前,Send() 函数是否会被阻塞?如果我使用线程锁定机制,例如 AutoResetEvent(s),默认情况下这会阻止 UI 线程吗?

大家好。

MSDN 说:

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.

是的。

SerialPort.DataReceived