SerialPort DataReceived事件线程

时间:2019-03-18 16:25:31

标签: c# serial-port

根据MSDN的DataReceived事件,它表示

  

从SerialPort对象接收数据时,在辅助线程上引发DataReceived事件。

这是否意味着只有一个辅助线程始终会触发该事件?还是SerialPort对象每次触发DataReceived事件时都会创建一个新线程?那么我在DataReceived事件中可以发起DataReceived事件吗?

我尝试用谷歌搜索,但找不到一个好的答案。

1 个答案:

答案 0 :(得分:1)

According to this post on the MSDN forums here.

When it does, data received in your case, it dips in the ThreadPool and calls QueueUserWorkItem() which allocates a thread pool thread to call your DataReceived event handler. It then immediately calls WaitCommEvent() again to wait for the next "something interesting".

It sounds like the SerialPort object uses a ThreadPool so although the DataReceived event is being called on different Threads, they already exist in the ThreadPool and therefor there is no overhead for creating new ones.

So it is possible to have the event fire even while you are in there because it will be on a different Thread.