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
synchronizableISynchronizableISynchronizable to provide SynchronizationContext to perform action.
actionActionAction.
allowReentrantboolTrue 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
synchronizableISynchronizableISynchronizable to provide SynchronizationContext to perform action.
asyncActionFunc<CancellationToken, Task>Asynchronous action.
allowReentrantboolTrue 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
synchronizableISynchronizableISynchronizable to provide SynchronizationContext to perform action.
asyncActionFunc<Task>Asynchronous action.
allowReentrantboolTrue to allow action being reentrant.
ScheduledAction(Action, bool)
Initialize new ScheduledAction instance with current SynchronizationContext.
public ScheduledAction(Action action, bool allowReentrant = false)
Parameters
ScheduledAction(Func<CancellationToken, Task>, bool)
Initialize new ScheduledAction instance with current SynchronizationContext.
public ScheduledAction(Func<CancellationToken, Task> asyncAction, bool allowReentrant = false)
Parameters
asyncActionFunc<CancellationToken, Task>Asynchronous action.
allowReentrantboolTrue 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
asyncActionFunc<Task>Asynchronous action.
allowReentrantboolTrue to allow action being reentrant.
ScheduledAction(SynchronizationContext, Action, bool)
Initialize new ScheduledAction instance.
public ScheduledAction(SynchronizationContext synchronizationContext, Action action, bool allowReentrant = false)
Parameters
synchronizationContextSynchronizationContextSynchronizationContext to perform action.
actionActionAction.
allowReentrantboolTrue 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
synchronizationContextSynchronizationContextSynchronizationContext to perform action.
asyncActionFunc<CancellationToken, Task>Asynchronous action.
allowReentrantboolTrue 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
synchronizationContextSynchronizationContextSynchronizationContext to perform action.
asyncActionFunc<Task>Asynchronous action.
allowReentrantboolTrue to allow action being reentrant.
Properties
IsAsynchronous
Check whether the action to be executed is asynchronous or not.
public bool IsAsynchronous { get; }
Property Value
IsExecuting
Check whether the action is currently being executed or not.
public bool IsExecuting { get; }
Property Value
IsScheduled
Check whether execution has been scheduled or not.
public bool IsScheduled { get; }
Property Value
SynchronizationContext
SynchronizationContext to perform action.
public SynchronizationContext SynchronizationContext { get; }
Property Value
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
tokenobjectToken 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
cancellationTokenCancellationTokenCancellationToken to cancel the action.
Returns
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
cancellationTokenCancellationToken
Returns
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
actionSendOrPostCallbackAction.
stateobjectState.
delayMillisintDelay 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
delayMillisintDelay time in milliseconds.
Reschedule(TimeSpan)
Reschedule execution. It will replace the previous scheduling.
public void Reschedule(TimeSpan delay)
Parameters
delayTimeSpanDelay 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
delayMillisintDelay time in milliseconds.
Schedule(TimeSpan)
Schedule execution. It won't be scheduled again if execution is already scheduled.
public void Schedule(TimeSpan delay)
Parameters
delayTimeSpanDelay time.
SendAction(SendOrPostCallback, object?)
Send action to underlying synchronization context and wait for completion.
protected virtual void SendAction(SendOrPostCallback action, object? state)
Parameters
actionSendOrPostCallbackAction.
stateobjectState.