Class TaskExecutor

Manages execution of asynchronus work using System.Threading.Tasks.Task API. If the task is associated with a TrackingToken then the task will be monitored by TaskMonitor and the outcome/status of the task can be retrieved from TaskMonitor.

Inheritance
System.Object
TaskExecutor
Namespace: EPiServer.Async
Assembly: EPiServer.Framework.dll
Version: 12.0.3
Syntax
public abstract class TaskExecutor : Object

Constructors

TaskExecutor()

Declaration
protected TaskExecutor()

Methods

Cancel(Guid)

Cancels the task with the specified identifier.

Declaration
public abstract Task Cancel(Guid taskId)
Parameters
Type Name Description
System.Guid taskId

The task identifier.

Returns
Type Description
System.Threading.Tasks.Task

The System.Threading.Tasks.Task object if the task is still running and can be cancelled; otherwise null.

OnTaskCompleted(TaskEventArgs)

Call to raise TaskCompleted event.

Declaration
protected void OnTaskCompleted(TaskEventArgs e)
Parameters
Type Name Description
TaskEventArgs e

The event argument

OnTaskStarted(TaskEventArgs)

Call to raise TaskStarted event.

Declaration
protected void OnTaskStarted(TaskEventArgs e)
Parameters
Type Name Description
TaskEventArgs e

The event argument

OnTaskStarting(TaskEventArgs)

Call to raise TaskStarting event.

Declaration
protected void OnTaskStarting(TaskEventArgs e)
Parameters
Type Name Description
TaskEventArgs e

The event argument

Start(Action)

Starts the specified work using an asynchronous task.

Declaration
public Task Start(Action work)
Parameters
Type Name Description
System.Action work

The work that should be executed asynchronously.

Returns
Type Description
System.Threading.Tasks.Task

An activated task

Start(Action, TrackingToken)

Starts the specified work using an asynchronous task and option to specify if outcome should be tracked.

Declaration
public Task Start(Action work, TrackingToken trackingToken)
Parameters
Type Name Description
System.Action work

The work that should be executed asynchronously.

TrackingToken trackingToken

The trackingtoken that will be associated with the task so it can be tracked.

Returns
Type Description
System.Threading.Tasks.Task

An activated task

Remarks

The trackingToken parameter can be used to specify a token that will be associated with the task. That can then be used to get progress or outcome of task from TaskMonitor.

Start(Action<CancellationToken>)

Starts the specified cancelable work using an asynchronous task.

Declaration
public Task Start(Action<CancellationToken> work)
Parameters
Type Name Description
System.Action<System.Threading.CancellationToken> work

The work that should be executed asynchronously.

Returns
Type Description
System.Threading.Tasks.Task

An activated task

Start(Action<CancellationToken>, TrackingToken)

Starts the specified cancelable work using an asynchronous task and option to specify if outcome should be tracked.

Declaration
public Task Start(Action<CancellationToken> work, TrackingToken trackingToken)
Parameters
Type Name Description
System.Action<System.Threading.CancellationToken> work

The work that should be executed asynchronously.

TrackingToken trackingToken

The trackingtoken that will be associated with the task so it can be tracked.

Returns
Type Description
System.Threading.Tasks.Task

An activated task

Remarks

The trackingToken parameter can be used to specify a token that will be associated with the task. That can then be used to get progress or outcome of task from TaskMonitor.

Start(Action<CancellationToken>, TrackingToken, CancellationToken)

Starts the specified work using an asynchronous task with the option to cancel the work prematurely and to specify if outcome should be tracked.

Declaration
public Task Start(Action<CancellationToken> work, TrackingToken trackingToken, CancellationToken cancellationToken)
Parameters
Type Name Description
System.Action<System.Threading.CancellationToken> work

The work that should be executed asynchronously.

TrackingToken trackingToken

The trackingtoken that will be associated with the task so it can be tracked.

System.Threading.CancellationToken cancellationToken

The cancellation token that can be used to cancel the task.

Returns
Type Description
System.Threading.Tasks.Task

An activated task

Remarks

The cancellationToken parameter can be used to cancel the task externally, but it will not signal if the task is cancelled using the Cancel(Guid) method. To listen to cancellation from either token, use the optional System.Threading.CancellationToken passed as an argument to the work action.

The trackingToken parameter can be used to specify a token that will be associated with the task. That can then be used to get progress or outcome of task from TaskMonitor.

Note that a task only is marked as cancelled if the work method responds to the cancellation by throwing the correct exception, normally using the System.Threading.CancellationToken.ThrowIfCancellationRequested method. If this does not happen, the task status will be set to System.Threading.Tasks.TaskStatus.RanToCompletion.

Start(Action<CancellationToken>, CancellationToken)

Starts the specified work using an asynchronous task with the option to cancel the work prematurely.

Declaration
public Task Start(Action<CancellationToken> work, CancellationToken cancellationToken)
Parameters
Type Name Description
System.Action<System.Threading.CancellationToken> work

The work that should be executed asynchronously.

System.Threading.CancellationToken cancellationToken

The cancellation token that can be used to cancel the task.

Returns
Type Description
System.Threading.Tasks.Task

An activated task

Remarks

The cancellationToken parameter can be used to cancel the task externally, but it will not signal if the task is cancelled using the Cancel(Guid) method. To listen to cancellation from either token, use the optional System.Threading.CancellationToken passed as an argument to the work action.

Note that a task only is marked as cancelled if the work method responds to the cancellation by throwing the correct exception, normally using the System.Threading.CancellationToken.ThrowIfCancellationRequested method. If this does not happen, the task status will be set to System.Threading.Tasks.TaskStatus.RanToCompletion.

Start<TResult>(Func<TResult>, TrackingToken)

Starts the specified work using an asynchronous task and option to specify if outcome should be tracked.

Declaration
public Task<TResult> Start<TResult>(Func<TResult> work, TrackingToken trackingToken)
    where TResult : new()
Parameters
Type Name Description
System.Func<TResult> work

The work that should be executed asynchronously.

TrackingToken trackingToken

The trackingtoken that will be associated with the task so it can be tracked.

Returns
Type Description
System.Threading.Tasks.Task<TResult>

An activated task of type System.Threading.Tasks.Task<>

Type Parameters
Name Description
TResult
Remarks

The trackingToken parameter can be used to specify a token that will be associated with the task. That can then be used to get progress or outcome of task from TaskMonitor.

Start<TResult>(Func<CancellationToken, TResult>, TrackingToken)

Starts the specified cancelable work using an asynchronous task and option to specify if outcome should be tracked.

Declaration
public Task<TResult> Start<TResult>(Func<CancellationToken, TResult> work, TrackingToken trackingToken)
    where TResult : new()
Parameters
Type Name Description
System.Func<System.Threading.CancellationToken, TResult> work

The work that should be executed asynchronously.

TrackingToken trackingToken

The trackingtoken that will be associated with the task so it can be tracked.

Returns
Type Description
System.Threading.Tasks.Task<TResult>

An activated task of type System.Threading.Tasks.Task<>

Type Parameters
Name Description
TResult
Remarks

The trackingToken parameter can be used to specify a token that will be associated with the task. That can then be used to get progress or outcome of task from TaskMonitor.

Start<TResult>(Func<CancellationToken, TResult>, TrackingToken, CancellationToken)

Starts the specified work using an asynchronous task with the option to cancel the work prematurely and to specify if outcome should be tracked.

Declaration
public abstract Task<TResult> Start<TResult>(Func<CancellationToken, TResult> work, TrackingToken trackingToken, CancellationToken cancellationToken)
    where TResult : new()
Parameters
Type Name Description
System.Func<System.Threading.CancellationToken, TResult> work

The work that should be executed asynchronously.

TrackingToken trackingToken

The trackingtoken that will be associated with the task so it can be tracked.

System.Threading.CancellationToken cancellationToken

The cancellation token that can be used to cancel the task.

Returns
Type Description
System.Threading.Tasks.Task<TResult>

An activated task of type System.Threading.Tasks.Task<>

Type Parameters
Name Description
TResult
Remarks

The cancellationToken parameter can be used to cancel the task externally, but it will not signal if the task is cancelled using the Cancel(Guid) method. To listen to cancellation from either token, use the optional System.Threading.CancellationToken passed as an argument to the work action.

The trackingToken parameter can be used to specify a token that will be associated with the task. That can then be used to get progress or outcome of task from TaskMonitor.

Note that a task only is marked as cancelled if the work method responds to the cancellation by throwing the correct exception, normally using the System.Threading.CancellationToken.ThrowIfCancellationRequested method. If this does not happen, the task status will be set to System.Threading.Tasks.TaskStatus.RanToCompletion.

Events

TaskCompleted

Occurs when a task has completed regardless of status.

Declaration
public event EventHandler<TaskEventArgs> TaskCompleted
Event Type
Type Description
System.EventHandler<TaskEventArgs>
Remarks

The TaskCompleted event will not be raised until all event handlers attached to either TaskStarted and/or have completed.

TaskStarted

Occurs when a task has started.

Declaration
public event EventHandler<TaskEventArgs> TaskStarted
Event Type
Type Description
System.EventHandler<TaskEventArgs>
Remarks

The task work is run on a separate thread to any event handlers. Attached event handlers will also not affect the TaskCompleted event.

TaskStarting

Occurs when a new task is starting.

Declaration
public event EventHandler<TaskEventArgs> TaskStarting
Event Type
Type Description
System.EventHandler<TaskEventArgs>
Remarks

The task itself will not be started until all event handlers has been completed.

Extension Methods