TryExecuteTask Method

Task Parallel System.Threading

Attempts to execute the provided Task on this scheduler.

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

Syntax

Visual Basic (Declaration)
Protected Function TryExecuteTask ( _
	task As Task _
) As Boolean
C#
protected bool TryExecuteTask(
	Task task
)

Parameters

task
Type: System.Threading.Tasks..::.Task
A Task object to be executed.

Return Value

A Boolean that is true if task was successfully executed, false if it was not. A common reason for execution failure is that the task had previously been executed or is in the process of being executed by another thread.

Remarks

Scheduler implementations are provided with Task instances to be executed through either the QueueTask(Task) method or the TryExecuteTaskInline(Task, Boolean) method. When the scheduler deems it appropriate to run the provided task, TryExecuteTask(Task) should be used to do so. TryExecuteTask handles all aspects of executing a task, including action invocation, exception handling, state management, and lifecycle control.

TryExecuteTask(Task) must only be used for tasks provided to this scheduler by the .NET Framework infrastructure. It should not be used to execute arbitrary tasks obtained through custom mechanisms.

Exceptions

ExceptionCondition
System..::.InvalidOperationException The task is not associated with this scheduler.

See Also