Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Task Class

Represents an asynchronous operation.

Namespace:  System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<DebuggerDisplayAttribute("Id = {Id}, Status = {Status}, Method = {DebuggerDisplayMethodDescription}")> _
Public Class Task _
	Implements IAsyncResult, IDisposable

The Task type exposes the following members.

  NameDescription
Public methodTask(Action)Initializes a new Task with the specified action.
Public methodTask(Action, CancellationToken)Initializes a new Task with the specified action and CancellationToken.
Public methodTask(Action, TaskCreationOptions)Initializes a new Task with the specified action and creation options.
Public methodTask(Action(Of Object), Object)Initializes a new Task with the specified action and state.
Public methodTask(Action, CancellationToken, TaskCreationOptions)Initializes a new Task with the specified action and creation options.
Public methodTask(Action(Of Object), Object, CancellationToken)Initializes a new Task with the specified action, state, and options.
Public methodTask(Action(Of Object), Object, TaskCreationOptions)Initializes a new Task with the specified action, state, and options.
Public methodTask(Action(Of Object), Object, CancellationToken, TaskCreationOptions)Initializes a new Task with the specified action, state, and options.
Top

  NameDescription
Public propertyAsyncStateGets the state object supplied when the Task was created, or null if none was supplied.
Public propertyCreationOptionsGets the TaskCreationOptions used to create this task.
Public propertyStatic memberCurrentIdReturns the unique ID of the currently executing Task.
Public propertyExceptionGets the AggregateException that caused the Task to end prematurely. If the Task completed successfully or has not yet thrown any exceptions, this will return null.
Public propertyStatic memberFactoryProvides access to factory methods for creating Task and Task(Of TResult) instances.
Public propertyIdGets a unique ID for this Task instance.
Public propertyIsCanceledGets whether this Task instance has completed execution due to being canceled.
Public propertyIsCompletedGets whether this Task has completed.
Public propertyIsFaultedGets whether the Task completed due to an unhandled exception.
Public propertyStatusGets the TaskStatus of this Task.
Top

  NameDescription
Public methodContinueWith(Action(Of Task))Creates a continuation that executes asynchronously when the target Task completes.
Public methodContinueWith(Action(Of Task), CancellationToken)Creates a continuation that executes asynchronously when the target Task completes.
Public methodContinueWith(Action(Of Task), TaskContinuationOptions)Creates a continuation that executes according to the specified TaskContinuationOptions.
Public methodContinueWith(Action(Of Task), TaskScheduler)Creates a continuation that executes asynchronously when the target Task completes.
Public methodContinueWith(Action(Of Task), CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes according to the specified TaskContinuationOptions.
Public methodContinueWith(Of TResult)(Func(Of Task, TResult))Creates a continuation that executes asynchronously when the target Task completes.
Public methodContinueWith(Of TResult)(Func(Of Task, TResult), CancellationToken)Creates a continuation that executes asynchronously when the target Task completes.
Public methodContinueWith(Of TResult)(Func(Of Task, TResult), TaskContinuationOptions)Creates a continuation that executes according to the condition specified in continuationOptions.
Public methodContinueWith(Of TResult)(Func(Of Task, TResult), TaskScheduler)Creates a continuation that executes asynchronously when the target Task completes.
Public methodContinueWith(Of TResult)(Func(Of Task, TResult), CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes according to the condition specified in continuationOptions.
Public methodDisposeReleases all resources used by the current instance of the Task class.
Protected methodDispose(Boolean)Disposes the Task, releasing all of its unmanaged resources.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRunSynchronouslyRuns the Task synchronously on the current TaskScheduler.
Public methodRunSynchronously(TaskScheduler)Runs the Task synchronously on the TaskScheduler provided.
Public methodStartStarts the Task, scheduling it for execution to the current TaskScheduler.
Public methodStart(TaskScheduler)Starts the Task, scheduling it for execution to the specified TaskScheduler.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodWaitWaits for the Task to complete execution.
Public methodWait(CancellationToken)Waits for the Task to complete execution.
Public methodWait(Int32)Waits for the Task to complete execution.
Public methodWait(TimeSpan)Waits for the Task to complete execution.
Public methodWait(Int32, CancellationToken)Waits for the Task to complete execution.
Public methodStatic memberWaitAll(Task())Waits for all of the provided Task objects to complete execution.
Public methodStatic memberWaitAll(Task(), Int32)Waits for all of the provided Task objects to complete execution.
Public methodStatic memberWaitAll(Task(), CancellationToken)Waits for all of the provided Task objects to complete execution.
Public methodStatic memberWaitAll(Task(), TimeSpan)Waits for all of the provided Task objects to complete execution.
Public methodStatic memberWaitAll(Task(), Int32, CancellationToken)Waits for all of the provided Task objects to complete execution.
Public methodStatic memberWaitAny(Task())Waits for any of the provided Task objects to complete execution.
Public methodStatic memberWaitAny(Task(), Int32)Waits for any of the provided Task objects to complete execution.
Public methodStatic memberWaitAny(Task(), CancellationToken)Waits for any of the provided Task objects to complete execution.
Public methodStatic memberWaitAny(Task(), TimeSpan)Waits for any of the provided Task objects to complete execution.
Public methodStatic memberWaitAny(Task(), Int32, CancellationToken)Waits for any of the provided Task objects to complete execution.
Top

  NameDescription
Explicit interface implemetationPrivate propertyIAsyncResult.AsyncWaitHandleGets a WaitHandle that can be used to wait for the task to complete.
Explicit interface implemetationPrivate propertyIAsyncResult.CompletedSynchronouslyGets an indication of whether the operation completed synchronously.
Top

Task instances may be created in a variety of ways. The most common approach is by using the Task type's Factory property to retrieve a TaskFactory instance that can be used to create tasks for several purposes. For example, to create a Task that runs an action, the factory's StartNew method may be used:

// C#
var t = Task.Factory.StartNew(() => DoAction());

' Visual Basic
 Dim t = Task.Factory.StartNew(Function() DoAction())

For more complete examples, see Task Parallelism (Task Parallel Library).

The Task class also provides constructors that initialize the Task but that do not schedule it for execution. For performance reasons, TaskFactory's StartNew method should be the preferred mechanism for creating and scheduling computational tasks, but for scenarios where creation and scheduling must be separated, the constructors may be used, and the task's Start method may then be used to schedule the task for execution at a later time.

For operations that return values, the Task(Of TResult) class should be used.

For Debugger Developers

For developers implementing custom debuggers, several internal and private members of Task may be useful (these may change from release to release). The Int32 m_taskId field serves as the backing store for the Id property, however accessing this field directly from a debugger may be more efficient than accessing the same value through the property's getter method (the s_taskIdCounter Int32 counter is used to retrieve the next available ID for a Task). Similarly, the Int32 m_stateFlags field stores information about the current lifecycle stage of the Task, information also accessible through the Status property. The m_action System.Object field stores a reference to the Task's delegate, and the m_stateObject System.Object field stores the async state passed to the Task by the developer. Finally, for debuggers that parse stack frames, the InternalWait method serves a potential marker for when a Task is entering a wait operation.

The following example shows how to start a task by using the StartNew method:

Silverlight

Supported in: 5

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

All members of Task, except for Dispose, are thread-safe and may be used from multiple threads concurrently.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft