I need to do this kind of work:
Consider merging "Load images" and "Process images" into one TransformBlock
block. That way you have no trouble keeping the images of a single page together.
In order to achieve your concurrency limit goal, use a SemaphoreSlim
:
SemaphoreSlim processImageDopLimiter = new SemaphoreSlim(8);
//...
var page = ...; //TransformBlock block input
var images = GetImages(page);
ImageWithPage[] processedImages =
images
.AsParallel()
.Select(i => {
processImageDopLimiter.WaitOne();
var result = ProcessImage(i);
processImageDopLimiter.ReleaseOne();
return result;
})
.ToList();
return new { page, processedImages };
This will lead to quite a few threads blocked waiting. You can use an asynchronous version of this processing if you like. This is immaterial to the question.