I am writing a C# application to communicate via serial to a microcontroller. I have a few questions on how to handle received messages. Below is the code that I am using at the moment, It received the messages perfectly fine, but I cannot update the Form, or store the data anywhere outside of this method (because it is in another thread).
com.DataReceived += new SerialDataReceivedEventHandler(OnReceived);
public void OnReceived(object sender, SerialDataReceivedEventArgs c) // This is started in another thread...
{
com.DiscardOutBuffer();
try
{
test = com.ReadExisting();
MessageBox.Show(test);
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
When I attempt to alter the Form, or call another method from here this is the error message I receive: “Cross Thead operation not valid”.
I would like to be able to display the information elsewhere or even better yet place it into an array to later be stored as a file. Is there any way I can do this?
Thanks again!
You need to invoke on the main thread using
InvokeorBeginInvoke:Or you can factor out part of the event handler (like showing a message box) and invoke that instead.