What are the differences between ConcurrentQueue and BlockingCollection in .Net?
Why BlockingCollection is best for producer-consumer operation when it can be done through ConcurrentQueue? Do I have to improve anything in the following code?
MessageSlotMachineGameStartOrAndStatusUpdate msg;
while (!aCancellationToken.IsCancellationRequested)
{
try
{
this.isStillConsumingMsg = true;
Boolean takeResult = this.msgQueue.TryTake(out msg, this.msgConsumeTimeOut, aCancellationToken);
if (takeResult)
{
if (msg != null)
{
this.ProcessMessage(msg);
}
}
else
{
break;
}
}
catch (OperationCanceledException err)
{
EngineManager.AddExceptionLog(err, "Signal Operation Canceled");
}
catch (Exception err)
{
EngineManager.AddExceptionLog(err, "Signal exception");
}
finally
{
this.isStillConsumingMsg = false;
}
}
BlockingCollectionhas aTakemethod that would block the consumer if there is nothing to take, and wait for a producer side to provide an item.ConcurrentQueuelacks such method – if it is empty, the consumer would need to handle the wait, and the producer would need to provide a non-empty notification.