I highly recommend the TPL - Tasks Parallel Library - 'DataFlow
' library. It's a very good abstraction of the TPL itself, easy to use. I was in a situation where I had to parallelize the execution of a file-converter, which in a single instance-run used only 15% CPU. By parallelzing it, I was able to utilize 100% CPU and finish the conversion-job much, much quicker.
IT works with .NET 4.5 and onwards, and I believe I saw a .NET Core version, too. But here's the .NET 4.5 version: https://www.nuget.org/packages/Microsoft.Tpl.Dataflow.
Install with NuGet and look to the web for examples of use. Note that many of the examples deal with async-awaitable methods, but the library works quite well with synchronous tasks as well. I had no need for async use, so my inspiration-example below is synchronous tasks only:
public void ConvertFilesInFolder(string sourceFilesFolderPath)
{
string[] filePathsAndNames = getFilePathsAndNames(sourceFilesFolderPath);
var block = new ActionBlock<string>(foobar =>
{
ConvertAndMoveTheFile(foo);
}, new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 6
});
foreach (string filePathAndName in filePathsAndNames)
{
block.SendAsync(filePathAndName);
}
block.Complete();
block.Completion.Wait();
}
public void ConvertAndMoveTheFile(string filePathAndName)
{
try
{
ConvertFile(filePathAndName);
moveOriginalFileToArchive(filePathAndName);
}
catch (Exception ex)
{
}
}
I found this blog-post very helpful in getting introduced and started with the library.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.