Search code examples
c#blocking

How to block an operation until a condition is met?


I am running this code and it is using a fair amount of CPU even though it is doing absolutely nothing most of the time.

while (this.IsListening)
{
    while (this.RecievedMessageBuffer.Count > 0)
    {
        lock (this.RecievedMessageBuffer)
        {
            this.RecievedMessageBuffer[0].Reconstruct();
            this.RecievedMessageBuffer[0].HandleMessage(messageHandler);
            this.RecievedMessageBuffer.RemoveAt(0);
        }
    }
}

What is the best way to block until a condition is met?


Solution

  • Assuming you are using .NET 4, I'd suggest switching RecievedMessageBuffer to be a BlockingCollection. When you are putting messages into it, call it's Add method. When you want to retrieve a message, call it's Take or TryTake methods. Take will block the reading thread until a message is available, without burning CPU like 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);
        }
    }