|
-
Jul 9th, 2007, 04:30 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2.0] Thread and delegate woes
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[]
-
Jul 11th, 2007, 01:39 PM
#2
I wonder how many charact
Re: [2.0] Thread and delegate woes
You will find your answer here:
http://www.albahari.com/threading/pa...ynch_Delegates
Or you may find Background Worker to be a better fit (you never fully described what you're doing)
http://www.albahari.com/threading/pa...ckgroundWorker
-
Jul 11th, 2007, 03:45 PM
#3
Thread Starter
Fanatic Member
Re: [2.0] Thread and delegate woes
Many thanks for the links, they are very helpful. I should be able to make some progress now.
Just FYI, I want to get any data received from the serial port back onto the thread the class was created on (which happens to be the UI thread), so that when it is passed to my form, I (or anyone else that uses the class) do not have to check for .InvokeRequired. All the threading stuff is done within the class.
-
Jul 11th, 2007, 06:41 PM
#4
Re: [2.0] Thread and delegate woes
 Originally Posted by Andy_P
Just FYI, I want to get any data received from the serial port back onto the thread the class was created on (which happens to be the UI thread), so that when it is passed to my form, I (or anyone else that uses the class) do not have to check for .InvokeRequired. All the threading stuff is done within the class.
That's exactly what the Timers.Timer and FileSystemWatcher classes do. They have a SynchronizingObject property which, when set, is used to marshal calls to the desired thread. Generally speaking the SynchronizingObject will be a form so that the object raises its events on the UI thread. The code would look something like this:
vb.net Code:
If Me.SynchronizingObject Is Nothing Then
'Use a thread pool thread to raise the event.
ElseIf Me.SynchronizingObject.InvokeRequired Then
'Create a delegate to marshal the call to the thread that owns the synchronising object.
Else
'Raise the event on the current thread.
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|