First, I don't understand why are you trying to use PLINQ here. Enumerating a list of Task
s shouldn't take long, so I don't think you're going to gain anything from parallelizing it.
Now, to get the first Task
that already completed with true
, you can use the (non-blocking) IsCompleted
property:
var task = tasks.FirstOrDefault(t => t.IsCompleted && t.Result);
If you wanted to get a collection of Task
s, ordered by their completion, have a look at Stephen Toub's article Processing tasks as they complete. If you want to list those that return true
first, you would need to modify that code. If you don't want to modify it, you can use a version of this approach from Stephen Cleary's AsyncEx library.
Also, in the specific case in your question, you could “fix” your code by adding .WithMergeOptions(ParallelMergeOptions.NotBuffered)
to the PLINQ query. But doing so still wouldn't work most of the time and can waste threads a lot even when it does. That's because PLINQ uses a constant number of threads and partitioning and using Result
would block those threads most of the time.