The thread lock of the stream will be released quickly, and the Close() method of the SerialPort instance does not need to wait. It does not wait for the UI thread to return after executing the delegation. My method is that the DataReceived event handler updates the interface with this.BeginInvoke(). Before Closing the serial port and accepting data, make a judgment. Most methods on the Internet are to define two bool type tags, Listening and Closing. this.Invoke() in the DataReceived event handler always waits for the UI thread to execute the delegation, but at this time, the UI thread still stops at the Close() method of the SerialPort instance and waits for the DataReceived event handler to finish executing.Īt this time, the thread deadlock occurs, and both sides cannot be executed. When the worker thread finishes processing the data and prepares to update the interface, the problem comes. The UI thread will wait at the lock(stream) for the auxiliary thread to release the stream's thread lock. When the auxiliary thread calls the DataReceived event handler to process the serial port data but has not updated the interface, click the "close" button to call the Close() method of the SerialPort instance. When the SerialStream instance executes the Close() method, it will lock the instance itself.When the SerialPort instance executes the Close() method, it will first close the SerialStream instance inside the SerialPort instance.Because you want to access the ui resources, you need to use the invoke method to synchronize the ui. Get the number of bytes read by the serial port Find causeīased on the fundamental principle, I continue to find out the cause of the problem.įirst look at the code that causes the interface to get stuck: void comm_DataReceived(object sender, SerialDataReceivedEventArgs e) I feel that there is a better way, and the article is not very clear. I personally don't accept this method very much. Reference articles C ා serial port operation series (2) - introduction, why does my serial port program deadlock when closing the serial port? The solution of this paper is similar to most of the solutions on the Internet: define two bool type tags, Listening and Closing, and judge before Closing the serial port and receiving data. Reference Zhihu article How to judge which step of WinForm interface is stuck in the code?, click debug pause to view the ui thread function stack, directly locate the number of lines of the blocking code, and confirm that the problem occurs in the Close() method of the SerialPort class. Reference blog The reason of windows program interface stuck, get the reason of interface Deadlock: the main thread and other threads have deadlock due to resource or lock contention. When I close the serial port, the interface will be stuck. A few days ago, I used the SerialPort class to write a serial port test program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |