System.Threading.Tasks.Task<TResult> Class

Represents an asynchronous operation that can return a value.

See Also: Task<TResult> Members

Syntax

[System.Diagnostics.DebuggerDisplay("Id = {Id}, Status = {Status}, Result = {ResultAsString}")]
[System.Diagnostics.DebuggerTypeProxy(typeof(System.Threading.Tasks.TaskDebuggerView))]
public class Task<TResult> : Task

Type Parameters

TResult
Documentation for this section has not yet been entered.

Remarks

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

Example

// 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 System.Threading.Tasks.Task`1 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 Task.Start method may then be used to schedule the task for execution at a later time.

Requirements

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