Assuming you are using .NET 4, I suggest switching RecievedMessageBuffer
to BlockingCollection . When you insert messages into it, name it Add Method. When you want to receive a message, call it the Take or TryTake methods. Take will block the read stream until the message is available, without writing to the processor, as your original example.
// Somewhere else BlockingCollection<SomethingLikeAMessage> RecievedMessageBuffer = new BlockingCollection<SomethingLikeAMessage>(); // Something like this where your example was while (this.IsListening) { SomethingLikeAMessage message; if (RecievedMessageBuffer.TryTake(out message, 5000); { message.Reconstruct(); message.HandleMessage(messageHandler); } }
source share