简体   繁体   中英

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

I have a C# program which communicates over a serial port, using the System.IO.Ports.SerialPort class. In order to receive data, I am subscribing to its DataReceived event handler.

My question is, when data is received - does the DataReceived event run in its own thread, or does it remain synchronous? ie will the Send() function be blocked until DataReceived is complete? If I use thread locking mechanisms such as AutoResetEvent(s), by default will this block the UI thread?

Cheers guys.

MSDN says:

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.

So yes.

SerialPort.DataReceived

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM