Waiting for a single task to fail out of a List

2019-01-29 06:16发布

问题:

In my application I have a List<Task<Boolean>> that I Task.Wait[..] on to determine if they completed successfully (Result = true). Though if during my waiting a Task completes and returns a falsey value I want to cancel all other Task I am waiting on and do something based on this.

I have created two "ugly" methods to do this

// Create a CancellationToken and List<Task<..>> to work with
CancellationToken myCToken = new CancellationToken();
List<Task<Boolean>> myTaskList = new List<Task<Boolean>>();

//-- Method 1 --
    // Wait for one of the Tasks to complete and get its result
Boolean finishedTaskResult = myTaskList[Task.WaitAny(myTaskList.ToArray(), myCToken)].Result;

    // Continue waiting for Tasks to complete until there are none left or one returns false
    while (myTaskList.Count > 0 && finishedTaskResult)
    {
        // Wait for the next Task to complete
        finishedTaskResult = myTaskList[Task.WaitAny(myTaskList.ToArray(), myCToken)].Result;
        if (!finishedTaskResult) break;
    }
    // Act on finishTaskResult here

// -- Method 2 -- 
    // Create a label to 
    WaitForOneCompletion:
    int completedTaskIndex = Task.WaitAny(myTaskList.ToArray(), myCToken);

    if (myTaskList[completedTaskIndex].Result)
    {
        myTaskList.RemoveAt(completedTaskIndex);
        goto WaitForOneCompletion;
    }
    else
        ;// One task has failed to completed, handle appropriately 

I was wondering if there was a cleaner way to do this, possibly with LINQ?

回答1:

You can use the following method to take a sequence of tasks and create a new sequence of tasks that represents the initial tasks but returned in the order that they all complete:

public static IEnumerable<Task<T>> Order<T>(this IEnumerable<Task<T>> tasks)
{
    var taskList = tasks.ToList();

    var taskSources = new BlockingCollection<TaskCompletionSource<T>>();

    var taskSourceList = new List<TaskCompletionSource<T>>(taskList.Count);
    foreach (var task in taskList)
    {
        var newSource = new TaskCompletionSource<T>();
        taskSources.Add(newSource);
        taskSourceList.Add(newSource);

        task.ContinueWith(t =>
        {
            var source = taskSources.Take();

            if (t.IsCanceled)
                source.TrySetCanceled();
            else if (t.IsFaulted)
                source.TrySetException(t.Exception.InnerExceptions);
            else if (t.IsCompleted)
                source.TrySetResult(t.Result);
        }, CancellationToken.None, TaskContinuationOptions.PreferFairness, TaskScheduler.Default);
    }

    return taskSourceList.Select(tcs => tcs.Task);
}

Now that you have the ability to order the tasks based on their completion you can write the code basically exactly as your requirements dictate:

foreach(var task in myTaskList.Order())
    if(!await task)
        cancellationTokenSource.Cancel();


回答2:

Using Task.WhenAny implementation, you can create like an extension overload that receives a filter too.

This method returns a Task that will complete when any of the supplied tasks have completed and the result pass the filter.

Something like this:

static class TasksExtensions
{
    public static Task<Task<T>> WhenAny<T>(this IList<Task<T>> tasks, Func<T, bool> filter)
    {
        CompleteOnInvokePromiseFilter<T> action = new CompleteOnInvokePromiseFilter<T>(filter);

        bool flag = false;
        for (int i = 0; i < tasks.Count; i++)
        {
            Task<T> completingTask = tasks[i];

            if (!flag)
            {
                if (action.IsCompleted) flag = true;
                else if (completingTask.IsCompleted)
                {
                    action.Invoke(completingTask);
                    flag = true;
                }
                else completingTask.ContinueWith(t =>
                {
                    action.Invoke(t);
                });
            }
        }

        return action.Task;
    }
}

class CompleteOnInvokePromiseFilter<T>
{
    private int firstTaskAlreadyCompleted;
    private TaskCompletionSource<Task<T>> source;
    private Func<T, bool> filter;

    public CompleteOnInvokePromiseFilter(Func<T, bool> filter)
    {
        this.filter = filter;
        source = new TaskCompletionSource<Task<T>>();
    }

    public void Invoke(Task<T> completingTask)
    {
        if (completingTask.Status == TaskStatus.RanToCompletion && 
            filter(completingTask.Result) && 
            Interlocked.CompareExchange(ref firstTaskAlreadyCompleted, 1, 0) == 0)
        {
            source.TrySetResult(completingTask);
        }
    }

    public Task<Task<T>> Task { get { return source.Task; } }

    public bool IsCompleted { get { return source.Task.IsCompleted; } }
}

You can use this extension method like this:

List<Task<int>> tasks = new List<Task<int>>();    
...Initialize Tasks...

var task = await tasks.WhenAny(x => x % 2 == 0);

//In your case would be something like tasks.WhenAny(b => b);


回答3:

Jon Skeet, Stephen Toub, and myself all have variations on the "order by completion" approach.

However, I find that usually people don't need this kind of complexity, if they focus their attention a bit differently.

In this case, you have a collection of tasks, and want them canceled as soon as one of them returns false. Instead of thinking about it from a controller perspective ("how can the calling code do this"), think about it from the task perspective ("how can each task do this").

If you introduce a higher-level asynchronous operation of "do the work and then cancel if necessary", you'll find your calling code cleans up nicely:

public async Task DoWorkAndCancel(Func<CancellationToken, Task<bool>> work,
    CancellationTokenSource cts)
{
  if (!await work(cts.Token))
    cts.Cancel();
}

List<Func<CancellationToken, Task<bool>>> allWork = ...;
var cts = new CancellationTokenSource();
var tasks = allWork.Select(x => DoWorkAndCancel(x, cts));
await Task.WhenAll(tasks);