Removing this odd while(true) for checking the queue I was reviewing some WPF code I've written in C# last year.

The scenario is the following:


I've got a usercontrol that show usermessages and it's docked in the bottom of the application's workspace
I've got a ConcurrentQueue that receives message via a IMessageMediator (from Catel, but's irrilevant how data are pushed)
I've a workerprocess that checks if there's some item in the queue and add them to a list that's bound to a grid

private readonly ConcurrentQueue&lt;UserMessage&gt; queue;
private readonly List&lt;UserMessage&gt; dataItems;

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    while (true)
    {
        if (!queue.IsEmpty)
        {
            UserMessage message;
            while (queue.TryDequeue(out message))
            {
                dataItems.Add(message);
            }
            RaisePropertyChanged(() =&gt; FilteredDataItems);
            RaisePropertyChanged(() =&gt; ErrorCountMessage);
            RaisePropertyChanged(() =&gt; WarningsCountMessage);

        }
        Thread.Sleep(500);
    }
}


//That's used to show only filtered items (Error/Warning....)

public IEnumerable&lt;UserMessage&gt; FilteredDataItems
{
    get
    {
        if (!enabledFilters.Any())
            return dataItems;
        return dataItems.Where(x =&gt; enabledFilters.Contains(x.LogMessageType));
    }
}



How can I remove this while(true) and the ugly/odd Thread.Sleep(500)?
It sounds like you are sleeping only to avoiding busy waiting on the queue too frequently.

How about following the suggestion in this SO answer:

https://stackoverflow.com/a/5014271/2268996

so that you use the blocking Take method in your worker with code like this:

while (true)
{
    UserMessage m = blockingQueue.Take();
    dataItems.add(m);
    RaisePropertyChanged(() =&gt; FilteredDataItems);
    ... (no need to sleep here)...
}


You will still have the while (true) but not the Thread.sleep(...) call. Will that work for your situation?