[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Represents an asynchronous operation.
Namespace:
System.Threading.Tasks
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True, _
ExternalThreading := True)> _
Public Class Task _
Implements IAsyncResult, IDisposable
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true,
ExternalThreading = true)]
public class Task : IAsyncResult, IDisposable
[HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true,
ExternalThreading = true)]
public ref class Task : IAsyncResult, IDisposable
[<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true,
ExternalThreading = true)>]
type Task =
class
interface IAsyncResult
interface IDisposable
end
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())
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.
System..::.Object
System.Threading.Tasks..::.Task
System.Threading.Tasks..::.Task<(Of <(TResult>)>)
All members of Task, except for Dispose, are thread-safe and may be used from multiple threads concurrently.
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 4
.NET Framework Client Profile
Supported in: 4
Reference
Other Resources