I have tried to feed a dictionary with the values of a BlockingCollection
(...)
lock (blockingCol)
{
lock (cache)
{
cache = new Dictionary<Guid, customDocument>();
Parallel.ForEach(blockingCol,
(document) =>
{
lock (cache) //Same bug with or without this lock
cache.Add(document.Id, document);
});
blockingCol = new BlockingCollection<customDocument>();
(...)
}
(...)
}
(...)
The problem is sometimes the document is already in the cache
. But I don't understand why. I create a new cache
before and the BlockingCollection
should not dequeue the same document twice. But I think it's a problem of the Parallel
because when I use the code under this it's work but I need a multithreading system here.
(...)
lock (blockingCol)
{
lock (cache)
{
cache = new Dictionary<Guid, customDocument>();
while (blockingCol.Count > 0)
{
var doc = _blockingCol.Take();
cache.Add(doc.Id, doc);
}
blockingCol = new BlockingCollection<customDocument>();
(...)
}
(...)
}
(...)