Rockstar
Rockstar

Reputation: 33

Is there a way to trigger 2 tasks, await for one to complete (without cancelling the other task) and then set a time before returning result

I am implementing a new feature in already existing application and would like to know how best I can achieve the following:

  1. Create two tasks.
  2. Start them in parallel.
  3. Await and wait for one of them to complete.
  4. Once one of them completes, spun a timer for t seconds before returning the response. This is done because one of the task might run for a longer time than another.

I have the solution of #1 to #3 but #4.

List<Task> tasks = new List<Task>(length);
tasks.Add(CreateTask(get_data_source));
await Task.WhenAny(tasks);

I don't want to provide any timeout in #3. But I would like to wait till the completion of a task and then trigger a timer for t seconds. After t seconds, return the result (if both completed, then both else just the completed task).

Upvotes: 2

Views: 531

Answers (3)

Fabio
Fabio

Reputation: 32455

After first task complete, you can await for any of remaining tasks or delay task to complete .

This will give you possibility to await 5 seconds for other tasks to complete, if not, then return result.

var tasks = new List<Task>
{
    CreateTask(get_data_source),
    CreateTask(get_data_source2)
};

var completedTask = await Task.WhenAny(tasks);

await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(5000));

// Return result

Upvotes: 4

TheGeneral
TheGeneral

Reputation: 81523

Another example, using a CancellationToken and CancellationTokenSource.CancelAfter

var ts = new CancellationTokenSource();

var list = Enumerable.Range(0, 4)
                     .Select(x => DoStuff(ts.Token))
                     .ToList();

await Task.WhenAny(list);

ts.CancelAfter(1000);

await Task.WhenAll(list);

This will help if your tasks are using CancellationTokens to help shut them down gracefully

Upvotes: 0

Anna B
Anna B

Reputation: 7

This might be a good place for Task.WhenAll(). It will return when all of the tasks are completed. That way you can just wait for all of the tasks to respond instead of just waiting for an amount of time.

https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.whenall?view=netframework-4.8

Upvotes: 0

Related Questions