Expand
.NET Framework Class Library
Task(Of TResult) Class

Represents an asynchronous operation that can return a value.

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

'Declaration

<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True,  _
	ExternalThreading := True)> _
Public Class Task(Of TResult) _
	Inherits Task

Type Parameters

TResult

The type of the result produced by this Task(Of TResult).

Remarks

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

// C# 
var t = Task<int>.Factory.StartNew(() => GenerateResult());

' Visual Basic
Dim t = Task(Of Integer).Factory.StartNew(Function() GenerateResult()) 

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

The Task(Of TResult) class also provides constructors that initialize the task but that do not schedule it for execution. For performance reasons, the 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.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Inheritance Hierarchy

System.Object
  System.Threading.Tasks.Task
    System.Threading.Tasks.Task(Of TResult)
Thread Safety

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

Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4
Community ContentAdd
Page view tracker