C# Serial Communication Winform UI Freezing

175 Views Asked by At

I am working on an application where a full-duplex serial communication occurs (Read/Write). Specifically, I read 256 bytes of data from a port in every 40 ms and write back other 256 bytes.

In addition to this, I have bunch of TextBoxes in WinForm and when 256 bytes of data is received, I update each TextBox based on the received data. To be able to do that, I did the following in a WinForm class:

  1. Read the data in SerialDataReceivedEventHandler method (e.g. port.Read(readData, 0, 256))

  2. Call UpdateUI() method (within the SerialDataReceivedEventHandler method) which updates the UI elements based on the received data. (e.g. textBox1.Text = Convert.ToString(readData[0])

  3. After finishing the UpdateUI, write another 256 bytes of data to the port (e.g. port.Write(writeData, 0, 256))

My problem is that UI keeps freezing and I cannot display every read messages on UI since I don't use any threading or async mechanism. How can I make WinForm display every message without freezing?

0

There are 0 best solutions below