I hope somebody can help me out here, been struggling with this for a few days and not getting anywhere.
The following code is from a class to control a serial port. The DataReceived event of the serial port is raised on a different thread to the one that created the serial port object and writes to it. I want to marshall any data received back onto the original thread. As the class does not inherit from Control, I do not have Invoke methods or .InvokeRequired property. I have tried all manner of delegate configurations, and also tried to implement ISynchronizeinvoke, without any succes, as basically I am not sure what I am doing.
Any help MASSIVELY appreciated.
c# Code:
public void SendCommand(string cmd) { //This runs on the thread the class was created on. this._port.Write(cmd); } private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { //The DataReceived event of the serial port is raised on a different thread. byte[] data = new byte[this._port.BytesToRead]; int num = this._port.Read(data, 0, this._port.BytesToRead); //I want to pass 'data' to the GetBytes method, and run it on the same thread as SendCommand } private void GetBytes(byte[] data) { //Run this on the same thread as SendCommand. }
PS Not sure why there is corruption in the code snippet, but it should read byte[]





Reply With Quote