Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Thread and delegate woes

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [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:
    1. public void SendCommand(string cmd)
    2. {
    3.     //This runs on the thread the class was created on.
    4.     this._port.Write(cmd);
    5. }
    6.  
    7. private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    8. {
    9.     //The DataReceived event of the serial port is raised on a different thread.
    10.                    
    11.     byte[] data = new byte[this._port.BytesToRead];
    12.     int num = this._port.Read(data, 0, this._port.BytesToRead);
    13.  
    14.     //I want to pass 'data' to the GetBytes method, and run it on the same thread as SendCommand
    15. }
    16.  
    17. private void GetBytes(byte[] data)
    18. {
    19.     //Run this on the same thread as SendCommand.
    20. }


    PS Not sure why there is corruption in the code snippet, but it should read byte[]
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    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

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Thread and delegate woes

    Quote 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:
    1. If Me.SynchronizingObject Is Nothing Then
    2.     'Use a thread pool thread to raise the event.
    3. ElseIf Me.SynchronizingObject.InvokeRequired Then
    4.     'Create a delegate to marshal the call to the thread that owns the synchronising object.
    5. Else
    6.     'Raise the event on the current thread.
    7. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width