using System.Collections.Concurrent;
using System.Threading.Tasks.Dataflow;
using System.Threading.Tasks;
using System.Collections.Generic;
public static class Program
public static async Task Main()
var completeFiles = new ConcurrentQueue<string>();
var filesToProcess = new Queue<string>(CreateListOfFakeFilePaths());
var executionOptions = new ExecutionDataflowBlockOptions
MaxDegreeOfParallelism = 3,
var backgroundDispatcher = new ActionBlock<string>(filePath =>
Console.WriteLine($"Background Thread [{{Environment.CurrentManagedThreadId}}] | Processing [{filePath}]");
completeFiles.Enqueue(filePath);
for (var count = 0; filesToProcess.Count > 0; count++)
backgroundDispatcher.Post(filesToProcess.Dequeue());
Console.WriteLine("Parent Thread Sleeping");
Console.WriteLine("Parent Thread Resuming");
while (filesToProcess.Count > 0)
backgroundDispatcher.Post(filesToProcess.Dequeue());
backgroundDispatcher.Complete();
await backgroundDispatcher.Completion;
Console.WriteLine(string.Join("\n", completeFiles.ToArray()));
private static string[] CreateListOfFakeFilePaths(int howMany = 25)
var filePaths = new List<string>();
for (var i = 0; i < howMany; i++)
filePaths.Add($"/Path/To/Temp/{i}.png");
return filePaths.ToArray();