My program uses an event handler to receive data from my serial port. The idea is to get the data that the resulting text is then added to the text field (rx). I did not have this problem, but something has changed, and I cannot understand that. So now I am revising the processing method.
While loading my winform form, the last thing I do is
if (!serialPort1.IsOpen) { serialPort1.Open(); serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); }
Then I have an event handler
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { string indata1 = serialPort1.ReadExisting();
When I run the program, it stops at rx.AppendText(Environment.NewLine + indata1); and gives an error
invalidoperationexception was unhandled: "Control" accessed from except the thread on which it was created.
From what I was able to read, I suggest using invoke or BeginInvoke .
I have never had a problem adding text, so now I canโt understand why this is a problem. Also, from what I read when I called, I just do not understand.
Can someone help me figure out how to use an invoke instance for my situation? or maybe show me another way to add a text box?
source share