System.Threading.Tasks.Task

Represents an asynchronous task.

Summary

Inheritance

Direct Known Subclasses:System.Threading.Tasks.Task< T >

Properties

Exception
AggregateException
Gets the exceptions for the task, if there are any.
Factory
static TaskFactory
IsCanceled
bool
Gets whether the task was cancelled.
IsCompleted
bool
Gets whether the task has been completed with either an exception, cancellation, or a result.
IsFaulted
bool
Gets whether the task failed.

Public functions

ContinueWith(Action< Task > continuation)
Registers a continuation for the task that will run when the task is complete.
ContinueWith(Action< Task > continuation, CancellationToken cancellationToken)
Registers a continuation for the task that will run when the task is complete.
ContinueWith< T >(Func< Task, T > continuation)
Task< T >
Registers a continuation for the task that will run when the task is complete.
ContinueWith< T >(Func< Task, T > continuation, CancellationToken cancellationToken)
Task< T >
Registers a continuation for the task that will run when the task is complete.
Wait()
void
Blocks until the task is complete.

Public static functions

Delay(TimeSpan timespan)
Creates a task that will complete successfully after the given timespan.
FromResult< T >(T result)
Task< T >
Creates a new, completed task for the given result.
Run(Action toRun)
Executes an action asynchronously, returning a task that represents the operation.
Run< T >(Func< T > toRun)
Task< T >
Executes a function asynchronously, returning a task that represents the operation.
WhenAll(params Task[] tasks)
Creates a task that is complete when all of the provided tasks are complete.
WhenAll(IEnumerable< Task > tasks)
Creates a task that is complete when all of the provided tasks are complete.
WhenAll< T >(IEnumerable< Task< T >> tasks)
Task< T[]>
Creates a task that is complete when all of the provided tasks are complete.

Properties

Exception

AggregateException Exception

Gets the exceptions for the task, if there are any.

null otherwise.

Factory

static TaskFactory Factory

IsCanceled

bool IsCanceled

Gets whether the task was cancelled.

IsCompleted

bool IsCompleted

Gets whether the task has been completed with either an exception, cancellation, or a result.

IsFaulted

bool IsFaulted

Gets whether the task failed.

Public functions

ContinueWith

Task ContinueWith(
  Action< Task > continuation
)

Registers a continuation for the task that will run when the task is complete.

Details
Parameters
continuation
The continuation to run after the task completes. The function takes the completed task as an argument.
Returns
A new Task that is complete after both the task and the continuation are complete.

ContinueWith

Task ContinueWith(
  Action< Task > continuation,
  CancellationToken cancellationToken
)

Registers a continuation for the task that will run when the task is complete.

Details
Parameters
continuation
The continuation to run after the task completes.
cancellationToken
The cancellation token.
Returns
A new Task that is complete after both the task and the continuation are complete.

ContinueWith< T >

Task< T > ContinueWith< T >(
  Func< Task, T > continuation
)

Registers a continuation for the task that will run when the task is complete.

Details
Template Parameters
T
The type returned by the continuation.
Parameters
continuation
The continuation to run after the task completes. The function takes the completed task as an argument and can return a value.
Returns
A new Task that returns the value returned by the continuation after both the task and the continuation are complete.

ContinueWith< T >

Task< T > ContinueWith< T >(
  Func< Task, T > continuation,
  CancellationToken cancellationToken
)

Registers a continuation for the task that will run when the task is complete.

Details
Template Parameters
T
The type returned by the continuation.
Parameters
continuation
The continuation to run after the task completes. The function takes the completed task as an argument and can return a value.
cancellationToken
The cancellation token.
Returns
A new Task that returns the value returned by the continuation after both the task and the continuation are complete.

Wait

void Wait()

Blocks until the task is complete.

Public static functions

Delay

Task Delay(
  TimeSpan timespan
)

Creates a task that will complete successfully after the given timespan.

Details
Parameters
timespan
The amount of time to wait.
Returns
A new task.

FromResult< T >

Task< T > FromResult< T >(
  T result
)

Creates a new, completed task for the given result.

Details
Template Parameters
T
The result type of the task.
Parameters
result
Returns
A completed task.

Run

Task Run(
  Action toRun
)

Executes an action asynchronously, returning a task that represents the operation.

Details
Parameters
toRun
The action to run.
Returns
A task representing the asynchronous operation.

Run< T >

Task< T > Run< T >(
  Func< T > toRun
)

Executes a function asynchronously, returning a task that represents the operation.

Details
Template Parameters
T
The return type of the task.
Parameters
toRun
The function to run.
Returns
A task representing the asynchronous operation.

WhenAll

Task WhenAll(
  params Task[] tasks
)

Creates a task that is complete when all of the provided tasks are complete.

If any of the tasks has an exception, all exceptions raised in the tasks will be aggregated into the returned task. Otherwise, if any of the tasks is cancelled, the returned task will be cancelled.

Details
Parameters
tasks
The tasks to aggregate.
Returns
A task that is complete when all of the provided tasks are complete.

WhenAll

Task WhenAll(
  IEnumerable< Task > tasks
)

Creates a task that is complete when all of the provided tasks are complete.

If any of the tasks has an exception, all exceptions raised in the tasks will be aggregated into the returned task. Otherwise, if any of the tasks is cancelled, the returned task will be cancelled.

Details
Parameters
tasks
The tasks to aggregate.
Returns
A task that is complete when all of the provided tasks are complete.

WhenAll< T >

Task< T[]> WhenAll< T >(
  IEnumerable< Task< T >> tasks
)

Creates a task that is complete when all of the provided tasks are complete.

If any of the tasks has an exception, all exceptions raised in the tasks will be aggregated into the returned task. Otherwise, if any of the tasks is cancelled, the returned task will be cancelled. If all of the tasks succeed, the result of the returned task will be an array containing the results of all of the input tasks.

Details
Template Parameters
T
The result type of the tasks.
Parameters
tasks
The tasks to aggregate.
Returns
A task that is complete when all of the provided tasks are complete.

Send feedback about...

Need help? Visit our support page.