Task.Run Method (Action, CancellationToken)
Queues the specified work to run on the ThreadPool and returns a task handle for that work.
Assemblies: System.Threading.Tasks (in System.Threading.Tasks.dll)
mscorlib (in mscorlib.dll)
static member Run : action:Action * cancellationToken:CancellationToken -> Task
- Type: System.Action
The work to execute asynchronously
- Type: System.Threading.CancellationToken
A cancellation token that should be used to cancel the work
Return ValueType: System.Threading.Tasks.Task
A task that represents the work queued to execute in the ThreadPool.
The following example calls the method to create a task that iterates the files in the C:\Windows\System32 directory. The lambda expression calls the Parallel.ForEach method to add information about each file to a List(T) object. Each detached nested task invoked by the Parallel.ForEach loop checks the state of the cancellation token and, if cancellation is requested, calls the CancellationToken.ThrowIfCancellationRequested method. The CancellationToken.ThrowIfCancellationRequested method throws an OperationCanceledException exception that is handled in a catch block when the calling thread calls the Task.Wait method.