Passing communication data between classes and threads in C#
-
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 aQueue
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 tomyQueue.Count
. Thanks in advance and kind regards, Andrea -
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 aQueue
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 tomyQueue.Count
. Thanks in advance and kind regards, AndreaI 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!
-
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!