Table of Contents

Class ScheduledAction

Namespace
CarinaStudio.Threading
Assembly
CarinaStudio.AppBase.Core.dll

Scheduled action which will be performed by specific SynchronizationContext. This is a thread-safe class.

public class ScheduledAction : ISynchronizable
Inheritance
ScheduledAction
Implements
Derived
Inherited Members
Extension Methods

Constructors

ScheduledAction(ISynchronizable, Action, bool)

Initialize new ScheduledAction instance.

public ScheduledAction(ISynchronizable synchronizable, Action action, bool allowReentrant = false)

Parameters

synchronizable ISynchronizable

ISynchronizable to provide SynchronizationContext to perform action.

action Action

Action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(ISynchronizable, Func<CancellationToken, Task>, bool)

Initialize new ScheduledAction instance.

public ScheduledAction(ISynchronizable synchronizable, Func<CancellationToken, Task> asyncAction, bool allowReentrant = false)

Parameters

synchronizable ISynchronizable

ISynchronizable to provide SynchronizationContext to perform action.

asyncAction Func<CancellationToken, Task>

Asynchronous action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(ISynchronizable, Func<Task>, bool)

Initialize new ScheduledAction instance.

public ScheduledAction(ISynchronizable synchronizable, Func<Task> asyncAction, bool allowReentrant = false)

Parameters

synchronizable ISynchronizable

ISynchronizable to provide SynchronizationContext to perform action.

asyncAction Func<Task>

Asynchronous action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(Action, bool)

Initialize new ScheduledAction instance with current SynchronizationContext.

public ScheduledAction(Action action, bool allowReentrant = false)

Parameters

action Action

Action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(Func<CancellationToken, Task>, bool)

Initialize new ScheduledAction instance with current SynchronizationContext.

public ScheduledAction(Func<CancellationToken, Task> asyncAction, bool allowReentrant = false)

Parameters

asyncAction Func<CancellationToken, Task>

Asynchronous action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(Func<Task>, bool)

Initialize new ScheduledAction instance with current SynchronizationContext.

public ScheduledAction(Func<Task> asyncAction, bool allowReentrant = false)

Parameters

asyncAction Func<Task>

Asynchronous action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(SynchronizationContext, Action, bool)

Initialize new ScheduledAction instance.

public ScheduledAction(SynchronizationContext synchronizationContext, Action action, bool allowReentrant = false)

Parameters

synchronizationContext SynchronizationContext

SynchronizationContext to perform action.

action Action

Action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(SynchronizationContext, Func<CancellationToken, Task>, bool)

Initialize new ScheduledAction instance.

public ScheduledAction(SynchronizationContext synchronizationContext, Func<CancellationToken, Task> asyncAction, bool allowReentrant = false)

Parameters

synchronizationContext SynchronizationContext

SynchronizationContext to perform action.

asyncAction Func<CancellationToken, Task>

Asynchronous action.

allowReentrant bool

True to allow action being reentrant.

ScheduledAction(SynchronizationContext, Func<Task>, bool)

Initialize new ScheduledAction instance.

public ScheduledAction(SynchronizationContext synchronizationContext, Func<Task> asyncAction, bool allowReentrant = false)

Parameters

synchronizationContext SynchronizationContext

SynchronizationContext to perform action.

asyncAction Func<Task>

Asynchronous action.

allowReentrant bool

True to allow action being reentrant.

Properties

IsAsynchronous

Check whether the action to be executed is asynchronous or not.

public bool IsAsynchronous { get; }

Property Value

bool

IsExecuting

Check whether the action is currently being executed or not.

public bool IsExecuting { get; }

Property Value

bool

IsScheduled

Check whether execution has been scheduled or not.

public bool IsScheduled { get; }

Property Value

bool

SynchronizationContext

SynchronizationContext to perform action.

public SynchronizationContext SynchronizationContext { get; }

Property Value

SynchronizationContext

Methods

Cancel()

Cancel scheduled execution.

public bool Cancel()

Returns

bool

True if action has been cancelled.

CancelAction(object)

Cancel posted action.

protected virtual bool CancelAction(object token)

Parameters

token object

Token returned from PostAction(SendOrPostCallback, object?, int) to identify the posted action.

Returns

bool

True if action has been cancelled successfully.

Execute()

Execute action on current thread immediately. The scheduled execution will be cancelled.

public bool Execute()

Returns

bool

True if the action has been executed.

ExecuteAsync(CancellationToken)

Execute action on current thread asynchronously. The scheduled execution will be cancelled.

public Task<bool> ExecuteAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

CancellationToken to cancel the action.

Returns

Task<bool>

Task of execution. The result will be True if action has been executed.

ExecuteAsyncIfScheduled(CancellationToken)

Execute action on current thread asynchronously if execution has been scheduled. The scheduled execution will be cancelled.

public Task<bool> ExecuteAsyncIfScheduled(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<bool>

Task of execution. The result will be True if action has been executed.

ExecuteIfScheduled()

Execute action on current thread immediately if execution has been scheduled. The scheduled execution will be cancelled.

public bool ExecuteIfScheduled()

Returns

bool

True if action has been executed.

PostAction(SendOrPostCallback, object?, int)

Post action to underlying synchronization context.

protected virtual object PostAction(SendOrPostCallback action, object? state, int delayMillis)

Parameters

action SendOrPostCallback

Action.

state object

State.

delayMillis int

Delay time in milliseconds.

Returns

object

Token to identify the posted action.

Reschedule()

Reschedule execution. It will replace the previous scheduling.

public void Reschedule()

Reschedule(int)

Reschedule execution. It will replace the previous scheduling.

public void Reschedule(int delayMillis)

Parameters

delayMillis int

Delay time in milliseconds.

Reschedule(TimeSpan)

Reschedule execution. It will replace the previous scheduling.

public void Reschedule(TimeSpan delay)

Parameters

delay TimeSpan

Delay time.

Schedule()

Schedule execution. It won't be scheduled again if execution is already scheduled.

public void Schedule()

Schedule(int)

Schedule execution. It won't be scheduled again if execution is already scheduled.

public void Schedule(int delayMillis)

Parameters

delayMillis int

Delay time in milliseconds.

Schedule(TimeSpan)

Schedule execution. It won't be scheduled again if execution is already scheduled.

public void Schedule(TimeSpan delay)

Parameters

delay TimeSpan

Delay time.

SendAction(SendOrPostCallback, object?)

Send action to underlying synchronization context and wait for completion.

protected virtual void SendAction(SendOrPostCallback action, object? state)

Parameters

action SendOrPostCallback

Action.

state object

State.