What is the evaluation order in a PLINQ query?

The following is an example of a PLINQ query that I run periodically in a Windows service:

var resultList = new List<Task<SendMailResult>>();

try
{
    resultList = emailsToSend
        .AsParallel().WithDegreeOfParallelism(10)
        .Select(async e =>
        {
            bool bSuccess = false;

            if (await MailHelper.SendMailAsync(e.sTo, e.sSubject, e.sHTML) == true)
            {
                bSuccess = true;
            }

            return new SendMailResult
            {
                succeeded = bSuccess,
                resultid = e.id
            };
        }).ToList();

    Task.WaitAll(resultList.ToArray());
}
catch (AggregateException aggEx)
{
    foreach (var ex in aggEx.InnerExceptions)
        Console.Out.WriteLine(ex.Message);
}

My question is: if the exception should be thrown inside anonymous async Func<EmailToSend, Task<SendMailResult>>(when calling MailHelper.SendMailAsync (), most likely), and therefore the AggregateException handler will be called - will the next .ToList()be called at any time?

In other words, is it possible that some of the tasks were completed successfully until they were completed, and after the code snippet below after you caught in the AggregateException handler, I can have a resultList with a non-0 Count? Or does an exception mean that ToList () is never called, and resultList will always be empty if an exception is raised?

, , . ! .

+4
1

.ToList() - Select , .
.ToList(). Select IEnumerable, .
, , , Faulted.

Edit:
Task.WaitAll, .

Update:
Task.WaitAll :
, , .
, .
, AggregateException.

+1

Source: https://habr.com/ru/post/1627981/


All Articles