# ScheduledTask Methods

[Type overview](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/)

## AbortAsync(string, CancellationToken) {#abortasync-string-system-threading-cancellationtoken-eb906763}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/abortasync-string-system-threading-cancellationtoken-eb906763/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L209-L210)

```csharp
public ValueTask AbortAsync(string reason, CancellationToken cancellationToken = default(CancellationToken))
```

Administratively aborts the task: the host terminalizes it and its attached descendants as failed with a [DurableTaskAbortedException](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskabortedexception/) carrying `reason`, without running further task code.

### Parameters

- `reason` (`string`): The reason for the abort, recorded as the exception message.
- `cancellationToken` (`CancellationToken`): A token which abandons waiting for the abort to commit.

### Exceptions

- `System.ArgumentException`: `reason` is `null`, empty, or whitespace.
- `System.NotSupportedException`: The host doesn't support aborting tasks.

## AbortAsyncCore(string, CancellationToken) {#abortasynccore-string-system-threading-cancellationtoken-da9ab5e3}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/abortasynccore-string-system-threading-cancellationtoken-da9ab5e3/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L216)

```csharp
protected virtual ValueTask AbortAsyncCore(string reason, CancellationToken cancellationToken)
```

Asks the host to abort the task for a validated `reason`.

### Parameters

- `reason` (`string`)
- `cancellationToken` (`CancellationToken`)

## CancelAsync(CancellationToken) {#cancelasync-system-threading-cancellationtoken-4e974282}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/cancelasync-system-threading-cancellationtoken-4e974282/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L175)

```csharp
public ValueTask CancelAsync(CancellationToken cancellationToken = default(CancellationToken))
```

Durably requests cancellation.

### Parameters

- `cancellationToken` (`CancellationToken`)

## CancelAsync(string?, CancellationToken) {#cancelasync-string-nullable-system-threading-cancellationtoken-75c5e1ac}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/cancelasync-string-nullable-system-threading-cancellationtoken-75c5e1ac/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L188)

```csharp
public ValueTask CancelAsync(string? reason, CancellationToken cancellationToken = default(CancellationToken))
```

Durably requests cancellation carrying `reason`.

### Parameters

- `reason` (`string?`): The reason which the task's [DurableTaskContext](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.durabletaskcontext/) reports, or `null` for none.
- `cancellationToken` (`CancellationToken`): A token which abandons waiting for the request to commit.

## CancelAsyncCore(string?, CancellationToken) {#cancelasynccore-string-nullable-system-threading-cancellationtoken-7d62358d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/cancelasynccore-string-nullable-system-threading-cancellationtoken-7d62358d/)

```csharp
protected abstract ValueTask CancelAsyncCore(string? reason, CancellationToken cancellationToken)
```

Asks the host to cancel the task carrying `reason`, or no reason when it is `null`.

### Parameters

- `reason` (`string?`)
- `cancellationToken` (`CancellationToken`)

## GetAwaiter {#getawaiter-f01bee9f}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/getawaiter-f01bee9f/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L76)

```csharp
public ScheduledTaskAwaiter GetAwaiter()
```

Gets an awaiter which waits for successful completion.

## GetFeature {#getfeature-1-8b232eb9}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/getfeature-1-8b232eb9/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L48)

```csharp
public virtual TFeature? GetFeature<TFeature>()
```

Returns the feature `TFeature` through which the task's host describes the task, or `null` when the host provides none.

### Returns

The feature, or `null`.

## GetResponseAsync(CancellationToken) {#getresponseasync-system-threading-cancellationtoken-2c816b77}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/getresponseasync-system-threading-cancellationtoken-2c816b77/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L95)

```csharp
public Task<DurableTaskResponse> GetResponseAsync(CancellationToken cancellationToken = default(CancellationToken))
```

Waits for and returns the terminal response.

### Parameters

- `cancellationToken` (`CancellationToken`)

## GetResponseAsync(PollingOptions, CancellationToken) {#getresponseasync-orleans-durable-tasks-pollingoptions-system-threading-cancellat-4f22fe00}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/getresponseasync-orleans-durable-tasks-pollingoptions-system-threading-cancellat-4f22fe00/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L101)

```csharp
public Task<DurableTaskResponse> GetResponseAsync(PollingOptions options, CancellationToken cancellationToken = default(CancellationToken))
```

Polls and returns the current response.

### Parameters

- `options` (`PollingOptions`)
- `cancellationToken` (`CancellationToken`)

### Exceptions

- `System.NotSupportedException`: The calling code runs in a durable task.

## GetStatusAsync(PollingOptions, CancellationToken) {#getstatusasync-orleans-durable-tasks-pollingoptions-system-threading-cancellatio-be523c56}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/getstatusasync-orleans-durable-tasks-pollingoptions-system-threading-cancellatio-be523c56/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L91)

```csharp
public Task<DurableTaskStatus> GetStatusAsync(PollingOptions options = default(PollingOptions), CancellationToken cancellationToken = default(CancellationToken))
```

Polls the current task status.

### Parameters

- `options` (`PollingOptions`)
- `cancellationToken` (`CancellationToken`)

### Exceptions

- `System.NotSupportedException`: The calling code runs in a durable task.

## IsCompletedAsync(PollingOptions, CancellationToken) {#iscompletedasync-orleans-durable-tasks-pollingoptions-system-threading-cancellat-59525d3d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/iscompletedasync-orleans-durable-tasks-pollingoptions-system-threading-cancellat-59525d3d/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs#L85)

```csharp
public Task<bool> IsCompletedAsync(PollingOptions options = default(PollingOptions), CancellationToken cancellationToken = default(CancellationToken))
```

Polls whether the task has reached a terminal state.

### Parameters

- `options` (`PollingOptions`)
- `cancellationToken` (`CancellationToken`)

### Exceptions

- `System.NotSupportedException`: The calling code runs in a durable task.

## PollAsyncCore(PollingOptions, CancellationToken) {#pollasynccore-orleans-durable-tasks-pollingoptions-system-threading-cancellation-065e3fa0}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/pollasynccore-orleans-durable-tasks-pollingoptions-system-threading-cancellation-065e3fa0/)

```csharp
protected abstract ValueTask<DurableTaskResponse> PollAsyncCore(PollingOptions options, CancellationToken cancellationToken)
```

Polls the current host response.

### Parameters

- `options` (`PollingOptions`)
- `cancellationToken` (`CancellationToken`)

## Race(IReadOnlyList&lt;ScheduledTask&lt;TResult&gt;&gt;, CancellationToken) {#race-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduledt-e13446ea}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/race-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduledt-e13446ea/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.Race.cs)

```csharp
public static Task<ScheduledTask<TResult>> Race<TResult>(IReadOnlyList<ScheduledTask<TResult>> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Waits for the first scheduled task to complete, requests durable cancellation of every other task, waits until every task is terminal, and returns the first task.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask<TResult>>`): The tasks, which are captured when this method is called.
- `cancellationToken` (`CancellationToken`): A token which abandons the wait. It doesn't cancel the tasks.

### Returns

The task which completed first, including a task which failed or was canceled. Every task has observed its terminal response, so [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), and `Orleans.Durable.Tasks.ScheduledTask-1.TryGetResult(-0@)` inspect the winner and the other tasks.

### Exceptions

- `System.ArgumentOutOfRangeException`: `tasks` is empty.

## Race(IReadOnlyList&lt;ScheduledTask&gt;, CancellationToken) {#race-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduledtas-63e0ea29}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/race-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduledtas-63e0ea29/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.Race.cs)

```csharp
public static Task<ScheduledTask> Race(IReadOnlyList<ScheduledTask> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Waits for the first scheduled task to complete, requests durable cancellation of every other task, waits until every task is terminal, and returns the first task.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask>`): The tasks, which are captured when this method is called.
- `cancellationToken` (`CancellationToken`): A token which abandons the wait. It doesn't cancel the tasks.

### Returns

The task which completed first, including a task which failed or was canceled. Every task has observed its terminal response, so [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), and `Orleans.Durable.Tasks.ScheduledTask-1.TryGetResult(-0@)` inspect the winner and the other tasks.

### Exceptions

- `System.ArgumentOutOfRangeException`: `tasks` is empty.

## WaitAsync(TimeSpan, CancellationToken) {#waitasync-system-timespan-system-threading-cancellationtoken-bb6c3ef4}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/waitasync-system-timespan-system-threading-cancellationtoken-bb6c3ef4/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public Task WaitAsync(TimeSpan timeout, CancellationToken cancellationToken = default(CancellationToken))
```

Waits for successful completion, and requests durable cancellation of the task once `timeout` elapses.

### Parameters

- `timeout` (`TimeSpan`): The time to wait before requesting cancellation, which is zero or positive.
- `cancellationToken` (`CancellationToken`): A token which abandons the wait. It doesn't cancel the task.

### Returns

An operation which completes once the task is terminal.

### Exceptions

- `System.ArgumentOutOfRangeException`: `timeout` is negative, including `System.Threading.Timeout.InfiniteTimeSpan`; await the task to wait without a timeout. Outside a durable execution, `timeout` also exceeds the longest timer interval which `System.Threading.CancellationTokenSource.CancelAfter(System.TimeSpan)` accepts.
- `System.TimeoutException`: The timeout elapsed, and the task ended canceled.

## WaitAsync(CancellationToken) {#waitasync-system-threading-cancellationtoken-bdb49003}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/waitasync-system-threading-cancellationtoken-bdb49003/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public ValueTask WaitAsync(CancellationToken cancellationToken = default(CancellationToken))
```

Waits for successful completion.

### Parameters

- `cancellationToken` (`CancellationToken`)

## WaitAsyncCore(CancellationToken) {#waitasynccore-system-threading-cancellationtoken-25bc13a8}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/waitasynccore-system-threading-cancellationtoken-25bc13a8/)

```csharp
protected internal abstract ValueTask<DurableTaskResponse> WaitAsyncCore(CancellationToken cancellationToken)
```

Waits for a terminal host response.

### Parameters

- `cancellationToken` (`CancellationToken`)

## WaitUntilAsync(DateTimeOffset, CancellationToken) {#waituntilasync-system-datetimeoffset-system-threading-cancellationtoken-86654bb5}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/waituntilasync-system-datetimeoffset-system-threading-cancellationtoken-86654bb5/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.Race.cs#L115)

```csharp
public Task WaitUntilAsync(DateTimeOffset dueTime, CancellationToken cancellationToken = default(CancellationToken))
```

Waits for successful completion, and requests durable cancellation of the task once `dueTime` passes.

### Parameters

- `dueTime` (`DateTimeOffset`): The time at which to request cancellation. A time which has passed times out promptly.
- `cancellationToken` (`CancellationToken`): A token which abandons the wait. It doesn't cancel the task.

### Returns

An operation which completes once the task is terminal.

### Exceptions

- `System.ArgumentOutOfRangeException`: Outside a durable execution, the time until `dueTime` exceeds the longest timer interval which `System.Threading.CancellationTokenSource.CancelAfter(System.TimeSpan)` accepts.
- `System.TimeoutException`: The due time passed, and the task ended canceled.

## WhenAll(IReadOnlyList&lt;ScheduledTask&gt;, CancellationToken) {#whenall-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduled-9beaba47}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/whenall-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduled-9beaba47/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static Task WhenAll(IReadOnlyList<ScheduledTask> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Waits for every scheduled task to complete successfully.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask>`)
- `cancellationToken` (`CancellationToken`)

## WhenAll(IReadOnlyList&lt;ScheduledTask&lt;TResult&gt;&gt;, CancellationToken) {#whenall-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedul-30edefb7}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/whenall-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedul-30edefb7/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static Task WhenAll<TResult>(IReadOnlyList<ScheduledTask<TResult>> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Waits for every scheduled task to complete successfully.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask<TResult>>`)
- `cancellationToken` (`CancellationToken`)

## WhenAllCompleted(IReadOnlyList&lt;ScheduledTask&lt;TResult&gt;&gt;, CancellationToken) {#whenallcompleted-1-system-collections-generic-ireadonlylist-orleans-durable-task-612a47e7}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/whenallcompleted-1-system-collections-generic-ireadonlylist-orleans-durable-task-612a47e7/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static Task<ScheduledTask<TResult>[]> WhenAllCompleted<TResult>(IReadOnlyList<ScheduledTask<TResult>> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Waits until every scheduled task is terminal, and returns the tasks in input order.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask<TResult>>`): The tasks, which are captured when this method is called.
- `cancellationToken` (`CancellationToken`): A token which abandons the wait. It doesn't cancel the tasks.

### Returns

The tasks, in input order. Each has observed its terminal response, so [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), and `Orleans.Durable.Tasks.ScheduledTask-1.TryGetResult(-0@)` inspect it.

## WhenAllCompleted(IReadOnlyList&lt;ScheduledTask&gt;, CancellationToken) {#whenallcompleted-system-collections-generic-ireadonlylist-orleans-durable-tasks-9b2512c0}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/whenallcompleted-system-collections-generic-ireadonlylist-orleans-durable-tasks-9b2512c0/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static Task<ScheduledTask[]> WhenAllCompleted(IReadOnlyList<ScheduledTask> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Waits until every scheduled task is terminal, and returns the tasks in input order.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask>`): The tasks, which are captured when this method is called.
- `cancellationToken` (`CancellationToken`): A token which abandons the wait. It doesn't cancel the tasks.

### Returns

The tasks, in input order. Each has observed its terminal response, so [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), [ScheduledTask](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/), and `Orleans.Durable.Tasks.ScheduledTask-1.TryGetResult(-0@)` inspect it.

## WhenAny(IReadOnlyList&lt;ScheduledTask&lt;TResult&gt;&gt;, CancellationToken) {#whenany-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedul-36415011}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/whenany-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedul-36415011/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static Task<ScheduledTask<TResult>> WhenAny<TResult>(IReadOnlyList<ScheduledTask<TResult>> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Returns the first scheduled task whose response completes, including failed or canceled durable responses. Wait cancellation and transport failures are propagated, and losing waits are canceled without canceling the durable tasks. Candidates are captured when this method is called.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask<TResult>>`)
- `cancellationToken` (`CancellationToken`)

### Exceptions

- `System.ArgumentOutOfRangeException`: `tasks` is empty.
- `System.InvalidOperationException`: The host's wait for the selected task returned an incomplete response.

## WhenAny(IReadOnlyList&lt;ScheduledTask&gt;, CancellationToken) {#whenany-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduled-6e355960}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/whenany-system-collections-generic-ireadonlylist-orleans-durable-tasks-scheduled-6e355960/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static Task<ScheduledTask> WhenAny(IReadOnlyList<ScheduledTask> tasks, CancellationToken cancellationToken = default(CancellationToken))
```

Returns the first scheduled task whose response completes, including failed or canceled durable responses. Wait cancellation and transport failures are propagated, and losing waits are canceled without canceling the durable tasks. Candidates are captured when this method is called.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask>`)
- `cancellationToken` (`CancellationToken`)

### Exceptions

- `System.ArgumentOutOfRangeException`: `tasks` is empty.
- `System.InvalidOperationException`: The host's wait for the selected task returned an incomplete response.

## WhenEach(IReadOnlyList&lt;ScheduledTask&lt;TResult&gt;&gt;) {#wheneach-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedu-9e7f95d7}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/wheneach-1-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedu-9e7f95d7/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static IAsyncEnumerable<ScheduledTask<TResult>> WhenEach<TResult>(IReadOnlyList<ScheduledTask<TResult>> tasks)
```

Yields each scheduled task once, in completion order, as it reaches a terminal state.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask<TResult>>`): The tasks, which are captured when this method is called.

### Returns

The tasks in completion order. Each yielded task has observed its terminal response, including a failure or a cancellation, which doesn't fail the enumeration.

## WhenEach(IReadOnlyList&lt;ScheduledTask&gt;) {#wheneach-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedule-9d17eabc}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.durable.abstractions/orleans.durable.tasks.scheduledtask/methods/wheneach-system-collections-generic-ireadonlylist-orleans-durable-tasks-schedule-9d17eabc/)

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable.Abstractions/ScheduledTask.cs)

```csharp
public static IAsyncEnumerable<ScheduledTask> WhenEach(IReadOnlyList<ScheduledTask> tasks)
```

Yields each scheduled task once, in completion order, as it reaches a terminal state.

### Parameters

- `tasks` (`IReadOnlyList<ScheduledTask>`): The tasks, which are captured when this method is called.

### Returns

The tasks in completion order. Each yielded task has observed its terminal response, including a failure or a cancellation, which doesn't fail the enumeration.
