This article discusses the producer-consumer pattern in .NET, some reasons why we should use it and demonstrates some examples of how to implement it in .NET.
This article is published at the .NET Curry magazine, click here to read the article.
This article discusses the producer-consumer pattern in .NET, some reasons why we should use it and demonstrates some examples of how to implement it in .NET.
This article is published at the .NET Curry magazine, click here to read the article.
var producers = Enumerable.Range(0, maxRequest)
.Select(_ => Task.Run(() =>
{
foreach (var tfDocument in tfDocumentToBlockingCollection.GetConsumingEnumerable())
{
var objectSpaceRebuilt = objectSpace.Rebuild(tfDocument, true, false, false);
queue.Add(objectSpaceRebuilt);
perrysMenu.Percentage += resultPorcentage + resultPorcentage;
}
})).ToArray();
var consume = Task.Run(() =>
{
foreach (var objectSpaceQueue in queue.GetConsumingEnumerable())
{
SendExportationRequest(objectSpaceQueue);
}
});
The code above, I have a question. how can I consume many ” var consume” in that example? I mean, I used in produce a Range(0, maxRequest), but I would like to know how can I do the same in consuming.
You can simply create more tasks. Take a look at the ProcessDocumentsUsingPipelinePattern method for an example.