Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing communication data between classes and threads in C#

Passing communication data between classes and threads in C#

Scheduled Pinned Locked Moved C#
csharpdesigndata-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Metal76
    wrote on last edited by
    #1

    Hi to all the CodeProject gurus. I'm quite new to OOP and C#, and when I face a new design problem I wonder if there could already be a "standard", commonly used way to solve it, but I don't have enough experience with C# to find it :-) The problem: I'm writing a class which parses incoming data bytes from the serial port looking for messages of a particular communication protocol. So, my parser works inside the SerialPort DataReceived event, processes all the incoming data and, when a new message is found, should be able to communicate it to the client application. My questions is: Is there a commonly used approach to pass data to the other classes, in this kind of situations? I was geared towards using a Queue of messages, so that new messages are added to the queue from the parser - myQueue.Enqueue(message) - and "consumed" by the application, which sits in a loop like:

    while(true)
    {
    if(myQueue.Count != 0)
    {
    message = myQueue.Dequeue();
    // Process message
    }

     // Avoids 100% processor occupation
     Thread.Sleep(10);
    

    }

    Is there any better way? And is this thread-safe? I remember that DataReceived works in a different thread from the main application, so I'm not sure about the call to myQueue.Count. Thanks in advance and kind regards, Andrea

    E 1 Reply Last reply
    0
    • M Metal76

      Hi to all the CodeProject gurus. I'm quite new to OOP and C#, and when I face a new design problem I wonder if there could already be a "standard", commonly used way to solve it, but I don't have enough experience with C# to find it :-) The problem: I'm writing a class which parses incoming data bytes from the serial port looking for messages of a particular communication protocol. So, my parser works inside the SerialPort DataReceived event, processes all the incoming data and, when a new message is found, should be able to communicate it to the client application. My questions is: Is there a commonly used approach to pass data to the other classes, in this kind of situations? I was geared towards using a Queue of messages, so that new messages are added to the queue from the parser - myQueue.Enqueue(message) - and "consumed" by the application, which sits in a loop like:

      while(true)
      {
      if(myQueue.Count != 0)
      {
      message = myQueue.Dequeue();
      // Process message
      }

       // Avoids 100% processor occupation
       Thread.Sleep(10);
      

      }

      Is there any better way? And is this thread-safe? I remember that DataReceived works in a different thread from the main application, so I'm not sure about the call to myQueue.Count. Thanks in advance and kind regards, Andrea

      E Offline
      E Offline
      engsrini
      wrote on last edited by
      #2

      I think this approach is Ok, but polling the queue every 10 ms is not a good idea, instead you can create a event in parent class (which holds serial port), the client(the one which is having while loop) can register for it, as soon as the data comes in SerialPort DataReceived event trigger the custom event to client class!

      M 1 Reply Last reply
      0
      • E engsrini

        I think this approach is Ok, but polling the queue every 10 ms is not a good idea, instead you can create a event in parent class (which holds serial port), the client(the one which is having while loop) can register for it, as soon as the data comes in SerialPort DataReceived event trigger the custom event to client class!

        M Offline
        M Offline
        Metal76
        wrote on last edited by
        #3

        Thank you for the suggestion, I ended up with an event driven approach as you suggested, and I'm passing data from the serial port event to the main thread using a mailbox based on an AutoResetEvent. Regards, Andrea

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups