Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am reading data from a serial port on the data received event of the serial port.

I define and open port on form load.

i can see the data and the program works when published but when debugging i get the following error :

Cross-thread operation not valid

at the following row (rTxtMass.AppendText(SPScale.ReadLine());)

rTxtMass is a richtextbox


C#
private void SPScale_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    rTxtMass.AppendText(SPScale.ReadLine());
}



Is it realy necessary to create a thread just to read data from a serial port?
The software package is only a remote display of a scale so security is not an issue.
Posted

1 solution

"Is it really necessary to create a thread just to read data from a serial port?"

No, you don't have to - but the system does.
The DataRecieved event is always on a different (non-UI) thread, which is why you get the Cross thread problem. All you have to do is Invoke it: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke(v=vs.110).aspx[^]
 
Share this answer
 
Comments
Dr Drastic 22-Jul-14 9:43am    
Thanks OriginalGriff,

Will implement the invoke.
OriginalGriff 22-Jul-14 9:59am    
You're welcome!
Dr Drastic 22-Jul-14 10:16am    
I Just Implemented the invoke and it works perfectly!
OriginalGriff 22-Jul-14 10:26am    
:thumbsup:!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900