Search code examples
c#.netasynchronousasync-awaittask-parallel-library

Is there default way to get first task that finished successfully?


Lets say that i have a couple of tasks:

void Sample(IEnumerable<int> someInts)
{
    var taskList = someInts.Select(x => DownloadSomeString(x));
}

async Task<string> DownloadSomeString(int x) {...}

I want to to get the result of first successful task. So, the basic solution is to write something like:

var taskList = someInts.Select(x => DownloadSomeString(x));
string content = string.Empty;
Task<string> firstOne = null;
while (string.IsNullOrWhiteSpace(content)){
    try
    {
        firstOne = await Task.WhenAny(taskList);
        if (firstOne.Status != TaskStatus.RanToCompletion)
        {
            taskList = taskList.Where(x => x != firstOne);
            continue;
        }
        content = await firstOne;
    }
    catch(...){taskList = taskList.Where(x => x != firstOne);}
}

But this solution seems to run N+(N-1)+..+K tasks. Where N is someInts.Count and K is position of first successful task in tasks, so as it's rerunning all task except one that is captured by WhenAny. So, is there any way to get first task that finished successfully with running maximum of N tasks? (if successful task will be the last one)


Solution

  • The problem with "the first successful task" is what to do if all tasks fail? It's a really bad idea to have a task that never completes.

    I assume you'd want to propagate the last task's exception if they all fail. With that in mind, I would say something like this would be appropriate:

    async Task<Task<T>> FirstSuccessfulTask(IEnumerable<Task<T>> tasks)
    {
      Task<T>[] ordered = tasks.OrderByCompletion();
      for (int i = 0; i != ordered.Length; ++i)
      {
        var task = ordered[i];
        try
        {
          await task.ConfigureAwait(false);
          return task;
        }
        catch
        {
          if (i == ordered.Length - 1)
            return task;
          continue;
        }
      }
      return null; // Never reached
    }
    

    This solution builds on the OrderByCompletion extension method that is part of my AsyncEx library; alternative implementations also exist by Jon Skeet and Stephen Toub.