Windows Dev Center

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.Run<TResult> Method (Func<TResult>, CancellationToken)

.NET Framework 4.6 and 4.5

Queues the specified work to run on the thread pool and returns a Task(TResult) handle for that work.

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

public static Task<TResult> Run<TResult>(
	Func<TResult> function,
	CancellationToken cancellationToken
)

Type Parameters

TResult

The result type of the task.

Parameters

function
Type: System.Func<TResult>

The work to execute asynchronously

cancellationToken
Type: System.Threading.CancellationToken

A cancellation token that should be used to cancel the work

Return Value

Type: System.Threading.Tasks.Task<TResult>
A Task(TResult) that represents the work queued to execute in the ThreadPool.

ExceptionCondition
ArgumentNullException

The function parameter was null.

TaskCanceledException

The task has been canceled.

ObjectDisposedException

The CancellationTokenSource associated with cancellationToken was disposed.

The Run method is a simpler alternative to the StartNew method. It creates a task with the following default values:

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft