Question

I'm considering making my own PLinq extension method to provide the feature described in my recent question: Specify task timeout in parallel linq to objects. I'd like to reuse as much existing .Net structure as I can, including any code that may already be specifying a Degree of Parallelism on the PLinq query.

In the below code block I'm specifying a degree of parallelism. Assuming that CustomForAll is a custom PLinq extension method with the same signature as the existing ForAll method, how would I read the degree of parallelism specified on the TSource?

private PictureList FetchPictures(List<Picture> wallResults) 
{                
        wallResults
            .AsParallel()
            .WithDegreeOfParallelism(10)
            .CustomForAll(delegate(Picture p){
Was it helpful?

Solution

Unfortunately, this isn't a supported operation. PLINQ was not designed in a manner that allows for third party extensions, without reverting to an IEnumerable<T>.

The degree of parallelism is stored within the ParallelQuery class in a private member variable (private QuerySettings m_specifiedSettings, in the int? m_degreeOfParallism field).

You could, potentially, use reflection to extract that, but it's an internal implementation detail that's subject to change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top