I'm trying to fix a problem in my Winforms application where the GUI tends to lock up.
Current Solution: The application is written to read from a serial port and write to a rich textbox in the GUI. Now, the response can be a single response or continuous streaming at high speed, based on the input .
As of now I'm updating the textbox with content as and when I receive it from the device,ie. with an event handler that triggers when data is received from the serial port.
Is background worker the only solution to the problem? If yes, how do I restructure my solution to accomodate this change?(I understand the background worker cannot access the GUI). If not, are there any better solutions?
EDIT for Code: Here is the general code flow
//Function triggered when data received from serial port
private void DataReceived(object sender, EventArgs e)
{
while (serialPort1.BytesToRead > 0 && serialPort1.IsOpen)
{
//calls to several processing functions
//which then call a function writetoTextBox and pass the data to write
}
}
//write to textbox
void writeToTextBox(inputdata)
{
// write to textbox.
//If user has asked to write to file. Open a file dialog to get file name and write to it
// as well. As of now when the data rate is high, this part doesnt get the time to respond
//as the GUI locks up the thread
}
Disclaimer: I'm relatively new to winforms and C# as such. So any advice would be appreciated!