Q:

Task. WhenAny for non faulted tasks

The description of the Task. WhenAny method says, that it will return the first task finished, even if it's faulted.  Is there a way to change this behavior, so it would return first successful task?  

A:

Something like this should do it (may need some tweaks - haven't tested):
private static async Task<Task> WaitForAnyNonFaultedTaskAsync(IEnumerable<Task> tasks)
{
    IList<Task> customTasks = tasks. ToList();
    Task completedTask;
    do
    {
        completedTask = await Task. WhenAny(customTasks);
        customTasks. Remove(completedTask);
    } while (completedTask. IsFaulted && customTasks. Count > 0);

    return completedTask. IsFaulted?null:completedTask;
}

