2 out of 4 rated this helpful Rate this topic

Task(Of TResult) Class

Represents an asynchronous operation that can return a value.

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

Namespace:  System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public class Task<TResult> : Task

Type Parameters

TResult

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

The Task(Of TResult) type exposes the following members.

  Name Description
Public method Task(Of TResult)(Func(Of TResult)) Initializes a new Task(Of TResult) with the specified function.
Public method Task(Of TResult)(Func(Of TResult), CancellationToken) Initializes a new Task(Of TResult) with the specified function.
Public method Task(Of TResult)(Func(Of TResult), TaskCreationOptions) Initializes a new Task(Of TResult) with the specified function and creation options.
Public method Task(Of TResult)(Func(Of Object, TResult), Object) Initializes a new Task(Of TResult) with the specified function and state.
Public method Task(Of TResult)(Func(Of TResult), CancellationToken, TaskCreationOptions) Initializes a new Task(Of TResult) with the specified function and creation options.
Public method Task(Of TResult)(Func(Of Object, TResult), Object, CancellationToken) Initializes a new Task(Of TResult) with the specified action, state, and options.
Public method Task(Of TResult)(Func(Of Object, TResult), Object, TaskCreationOptions) Initializes a new Task(Of TResult) with the specified action, state, and options.
Public method Task(Of TResult)(Func(Of Object, TResult), Object, CancellationToken, TaskCreationOptions) Initializes a new Task(Of TResult) with the specified action, state, and options.
Top
  Name Description
Public property AsyncState Gets the state object supplied when the Task was created, or null if none was supplied. (Inherited from Task.)
Public property CreationOptions Gets the TaskCreationOptions used to create this task. (Inherited from Task.)
Public property Exception Gets 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. (Inherited from Task.)
Public property Static member Factory Provides access to factory methods for creating Task(Of TResult) instances.
Public property Id Gets a unique ID for this Task instance. (Inherited from Task.)
Public property IsCanceled Gets whether this Task instance has completed execution due to being canceled. (Inherited from Task.)
Public property IsCompleted Gets whether this Task has completed. (Inherited from Task.)
Public property IsFaulted Gets whether the Task completed due to an unhandled exception. (Inherited from Task.)
Public property Result Gets the result value of this Task(Of TResult).
Public property Status Gets the TaskStatus of this Task. (Inherited from Task.)
Top
  Name Description
Public method ContinueWith(Action(Of Task)) Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public method ContinueWith(Action(Of Task(Of TResult))) Creates a continuation that executes asynchronously when the target Task(Of TResult) completes.
Public method ContinueWith(Action(Of Task), CancellationToken) Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public method ContinueWith(Action(Of Task), TaskContinuationOptions) Creates a continuation that executes according to the specified TaskContinuationOptions. (Inherited from Task.)
Public method ContinueWith(Action(Of Task), TaskScheduler) Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public method ContinueWith(Action(Of Task(Of TResult)), CancellationToken) Creates a continuation that executes asynchronously when the target Task(Of TResult) completes.
Public method ContinueWith(Action(Of Task(Of TResult)), TaskContinuationOptions) Creates a continuation that executes according the condition specified in continuationOptions.
Public method ContinueWith(Action(Of Task(Of TResult)), TaskScheduler) Creates a continuation that executes asynchronously when the target Task(Of TResult) completes.
Public method ContinueWith(Action(Of Task), CancellationToken, TaskContinuationOptions, TaskScheduler) Creates a continuation that executes according to the specified TaskContinuationOptions. (Inherited from Task.)
Public method ContinueWith(Action(Of Task(Of TResult)), CancellationToken, TaskContinuationOptions, TaskScheduler) Creates a continuation that executes according the condition specified in continuationOptions.
Public method ContinueWith(Of TResult)(Func(Of Task, TResult)) Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public method ContinueWith(Of TNewResult)(Func(Of Task(Of TResult), TNewResult)) Creates a continuation that executes asynchronously when the target Task(Of TResult) completes.
Public method ContinueWith(Of TResult)(Func(Of Task, TResult), CancellationToken) Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public method ContinueWith(Of TResult)(Func(Of Task, TResult), TaskContinuationOptions) Creates a continuation that executes according to the condition specified in continuationOptions. (Inherited from Task.)
Public method ContinueWith(Of TResult)(Func(Of Task, TResult), TaskScheduler) Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public method ContinueWith(Of TNewResult)(Func(Of Task(Of TResult), TNewResult), CancellationToken) Creates a continuation that executes asynchronously when the target Task(Of TResult) completes.
Public method ContinueWith(Of TNewResult)(Func(Of Task(Of TResult), TNewResult), TaskContinuationOptions) Creates a continuation that executes according the condition specified in continuationOptions.
Public method ContinueWith(Of TNewResult)(Func(Of Task(Of TResult), TNewResult), TaskScheduler) Creates a continuation that executes asynchronously when the target Task(Of TResult) completes.
Public method ContinueWith(Of TResult)(Func(Of Task, TResult), CancellationToken, TaskContinuationOptions, TaskScheduler) Creates a continuation that executes according to the condition specified in continuationOptions. (Inherited from Task.)
Public method ContinueWith(Of TNewResult)(Func(Of Task(Of TResult), TNewResult), CancellationToken, TaskContinuationOptions, TaskScheduler) Creates a continuation that executes according the condition specified in continuationOptions.
Public method Dispose Releases all resources used by the current instance of the Task class. (Inherited from Task.)
Protected method Dispose(Boolean) Disposes the Task, releasing all of its unmanaged resources. (Inherited from Task.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method RunSynchronously Runs the Task synchronously on the current TaskScheduler. (Inherited from Task.)
Public method RunSynchronously(TaskScheduler) Runs the Task synchronously on the TaskScheduler provided. (Inherited from Task.)
Public method Start Starts the Task, scheduling it for execution to the current TaskScheduler. (Inherited from Task.)
Public method Start(TaskScheduler) Starts the Task, scheduling it for execution to the specified TaskScheduler. (Inherited from Task.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Wait Waits for the Task to complete execution. (Inherited from Task.)
Public method Wait(CancellationToken) Waits for the Task to complete execution. (Inherited from Task.)
Public method Wait(Int32) Waits for the Task to complete execution. (Inherited from Task.)
Public method Wait(TimeSpan) Waits for the Task to complete execution. (Inherited from Task.)
Public method Wait(Int32, CancellationToken) Waits for the Task to complete execution. (Inherited from Task.)
Top
  Name Description
Explicit interface implemetation Private property IAsyncResult.AsyncWaitHandle Gets a WaitHandle that can be used to wait for the task to complete. (Inherited from Task.)
Explicit interface implemetation Private property IAsyncResult.CompletedSynchronously Gets an indication of whether the operation completed synchronously. (Inherited from Task.)
Top

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.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), 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.

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

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ