Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 671
Sic Avatar asked May 30 '16 15:05

Sic


People also ask

Why doesn’t this script specify the task sequence that was successfully executed?

In this case it was not necessary to also specify exactly what task sequence that had successfully been executed, so that’s why that’s not a part of this script. The Status Message ID that we’re using to accomplish this is 11143.

How do I tell my boss that I completed a task?

If the task required that you prepare some report you can always email it to him ask him if he would like to review it before you send it off to the final audience. If it’s not that sort of situation either tell him or email him you completed so and so and ask if there is anything else you can do. The last part is important. Shows initiative.

How do you write a good review of a task?

When you started, when you ended the tasking. List any shortcomings, or success you experienced. Maybe list what could possibly be done to improve the quality of the production of the tasking such as additional tools, additional time, or location, etc..

How do you write a well noted task in an email?

When you started, when you ended the tasking. List any shortcomings, or success you experienced. Maybe list what could possibly be done to improve the quality of the production of the tasking such as additional tools, additional time, or location, etc.. I hope this helps. How do I say “well noted” formally in an email?


2 Answers

All you need to do is create a TaskCompletionSource, add a continuation to each of your tasks, and set it when the first one finished successfully:

public static Task<T> FirstSuccessfulTask<T>(IEnumerable<Task<T>> tasks)
{
    var taskList = tasks.ToList();
    var tcs = new TaskCompletionSource<T>();
    int remainingTasks = taskList.Count;
    foreach (var task in taskList)
    {
        task.ContinueWith(t =>
            {
                if (task.Status == TaskStatus.RanToCompletion)
                    tcs.TrySetResult(t.Result);
                else
                if (Interlocked.Decrement(ref remainingTasks) == 0)
                    tcs.SetException(new AggregateException(tasks.SelectMany(t1 => t1.Exception.InnerExceptions)));
            });
    }
    return tcs.Task;
}

And a version for tasks without a result:

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

    var tcs = new TaskCompletionSource<bool>();

    int remainingTasks = taskList.Count;

    foreach (var task in taskList)
    {
        task.ContinueWith(t =>
        {
            if (task.Status == TaskStatus.RanToCompletion)
                tcs.TrySetResult(true);
            else
                if (Interlocked.Decrement(ref remainingTasks) == 0)
                tcs.SetException(new AggregateException(
                    tasks.SelectMany(t1 => t1.Exception.InnerExceptions)));
        });
    }

    return tcs.Task;
}
like image 154
Servy Avatar answered Sep 25 '22 23:09

Servy


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.

like image 38
Stephen Cleary Avatar answered Sep 24 '22 23:09

Stephen Cleary