Reputation: 755
Problem: I have filesystem where files appear and i want to upload them over WCF. What i want is to limit maximum number of parallelism to some ThreadMaxConcurrency
Idea is to utilize Producer-Consumer pattern in blockingQueue. Producing part is - UploadNewFile,CreateFolder etc...
What i am blind of is that consumer part. Also what i dont know is to some kind of... delay single task - For an example - DONT upload new files before FolderWasCreated for them.
I am using .NET 4.5 and i dont know how to utilize BlockingQueue and how to properly monitor if there is task and how to monitor and how to postpone some task until another one completes (enqueue them to the end again would work i guess).
Upvotes: 0
Views: 548
Reputation: 116538
You should use TPL Dataflow which is a framework that does all that for you. You create an AcionBlock
, give it a delegate and set it's MaxDegreeOfParallelism
.
It should look similar to this:
var block = new ActionBlock<string>(folderName =>
{
UploadFolder(folderName);
}, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 5 });
foreach (var folderName in GetFolderNames())
{
block.Post(folderName);
}
block.Complete();
await block.Completion;
Upvotes: 2