Class AsyncRelayCommandCommon
A reusable command that encapsulates the implementation of ICommand with support for async/await command delegates.
Enables instant creation of an ICommand without implementing the ICommand interface for each command.
The AsyncRelayCommandCommon<TParam> accepts asynchronous command handlers and supports data bindng to properties like IsExecuting by implementing INotifyPropertyChanged.
Call and await the ExecuteAsync() method or one of its overloads to execute the command explicitly asynchronously.
ICommand
Inherited Members
Namespace: BionicCode.Utilities.Net
Assembly: BionicCode.Utilities.Net.Common.dll
Syntax
public abstract class AsyncRelayCommandCommon : IAsyncRelayCommandCommon, ICommand, INotifyPropertyChanged
Remarks
AsyncRelayCommandCommon implements ICommand. In case the AsyncRelayCommandCommon<TParam> is executed explicitly, especially with an asynchronous command handler registered, it is highly recommended to invoke the awaitable ExecuteAsync() or its overloads instead.
Constructors
| Improve this Doc View SourceAsyncRelayCommandCommon()
Constructor
Declaration
protected AsyncRelayCommandCommon()
AsyncRelayCommandCommon(Action)
Creates a new parameterless synchronous command that can always execute (CanExecute() will always return true).
Declaration
protected AsyncRelayCommandCommon(Action executeNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | executeNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Action, Func<Boolean>)
Creates a new parameterless synchronous command.
Declaration
protected AsyncRelayCommandCommon(Action executeNoParam, Func<bool> canExecuteNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | executeNoParam | The execution handler. |
| Func<Boolean> | canExecuteNoParam | The execution status handler. |
AsyncRelayCommandCommon(Action<CancellationToken>)
Creates a new parameterless synchronous command that can always execute (CanExecute() will always return true)
and supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Action<CancellationToken> executeNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<CancellationToken> | executeNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Action<CancellationToken>, Func<Boolean>)
Creates a new parameterless synchronous command that supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Action<CancellationToken> executeCancellable, Func<bool> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<CancellationToken> | executeCancellable | The awaitable execution handler. |
| Func<Boolean> | canExecute | The can execute handler. |
AsyncRelayCommandCommon(Func<CancellationToken, Task>)
Creates a new parameterless asynchronous command that can always execute (CanExecute() will always return true)
and supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Func<CancellationToken, Task> executeAsyncNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<CancellationToken, Task> | executeAsyncNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Func<CancellationToken, Task>, Func<Boolean>)
Creates a new parameterless asynchronous command that supports cancellation and does not take a command parameter.
Declaration
protected AsyncRelayCommandCommon(Func<CancellationToken, Task> executeAsync, Func<bool> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<CancellationToken, Task> | executeAsync | The awaitable execution handler. |
| Func<Boolean> | canExecute | The can execute handler. |
AsyncRelayCommandCommon(Func<Task>)
Creates a new parameterless asynchronous command that can always execute (CanExecute() will always return true).
Declaration
protected AsyncRelayCommandCommon(Func<Task> executeAsyncNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | executeAsyncNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Func<Task>, Func<Boolean>)
Creates a parameterless new asynchronous command.
Declaration
protected AsyncRelayCommandCommon(Func<Task> executeAsyncNoParam, Func<bool> canExecuteNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | executeAsyncNoParam | The awaitable execution handler. |
| Func<Boolean> | canExecuteNoParam | The execution status handler. |
Fields
| Improve this Doc View SourceMaxThreads
The number of concurrent threads than can execute a command delegate.
Declaration
protected const int MaxThreads = 1
Field Value
| Type | Description |
|---|---|
| Int32 |
Properties
| Improve this Doc View SourceCanBeCanceled
Returns whether the command can be cancelled.
Declaration
public bool CanBeCanceled { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
|
CanExecuteNoParamDelegate
The registered parameterless CanExecute delegate.
Declaration
protected Func<bool> CanExecuteNoParamDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Func<Boolean> |
|
CurrentCancellationToken
The currently used CancellationToken.
Declaration
public CancellationToken CurrentCancellationToken { get; }
Property Value
| Type | Description |
|---|---|
| CancellationToken | A CancellationToken instance currently in use during the command execution. |
Remarks
This property can be used to query the state of the cancellation or to register a cancellation callback by calling Register(Action).
The instance will change for each command execution.
ExecuteAsyncNoParamDelegate
The registered parameterless async execute delegate.
Declaration
protected Func<Task> ExecuteAsyncNoParamDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Func<Task> | A delegate that takes no command parameter and returns a Task. |
ExecuteCancellableAsyncNoParamDelegate
The registered parameterless async execute delegate that supports cancellation.
Declaration
protected Func<CancellationToken, Task> ExecuteCancellableAsyncNoParamDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Func<CancellationToken, Task> | A delegate that supports cancellation, but takes no command parameter and returns a Task. |
ExecuteCancellableNoParamDelegate
The registered parameterless synchronous execute delegate that supports cancellation.
Declaration
protected Action<CancellationToken> ExecuteCancellableNoParamDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Action<CancellationToken> | A delegate that suppoprts cancellation, but takes no command parameter and returns void. |
ExecuteNoParamDelegate
The registered parameterless synchronous execute delegate.
Declaration
protected Action ExecuteNoParamDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Action | A delegate that takes no parameter and returns void. |
HasPending
Return whether the command has pending executions.
Declaration
public bool HasPending { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
Remarks
Only one command is executed at time. Sucessive command invocations are enqueued and run after the currently executing delegate has completed
or was cancelled.
IsCancelled
Returns whether the command's executing operation was cancelled.
Declaration
public bool IsCancelled { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
|
IsExecuting
A flag to signal if the asynchronous operation has completed.
Declaration
public bool IsExecuting { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
IsPendingCancelled
Returns whether the command's pending operations were cancelled.
Declaration
public bool IsPendingCancelled { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
|
PendingCount
Returns the number of pending command delegate executions.
Declaration
public int PendingCount { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
Methods
| Improve this Doc View SourceCancelAll()
Cancels all pending and executing commands.
Declaration
public void CancelAll()
Remarks
See Cancel() for the exception behavior of this overload.
CancelAll(Boolean)
Cancels all pending and executing commands.
Declaration
public void CancelAll(bool throwOnFirstException)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | throwOnFirstException | See Cancel(Boolean) for the effects of the parameter. |
Remarks
See Cancel(Boolean) for the exception behavior of this overload.
CancelExecuting()
Cancels the currently executing command.
Declaration
public void CancelExecuting()
Remarks
See Cancel() for the exception behavior of this overload.
CancelExecuting(Boolean)
Cancels the currently executing command.
Declaration
public void CancelExecuting(bool throwOnFirstException)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | throwOnFirstException | See Cancel(Boolean) for the effects of the parameter. |
Remarks
See Cancel(Boolean) for the exception behavior of this overload.
CancelPending()
Cancels all pending command executions.
Declaration
public void CancelPending()
Remarks
See Cancel() for the exception behavior of this overload.
CancelPending(Boolean)
Cancels all pending command executions.
Declaration
public void CancelPending(bool throwOnFirstException)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | throwOnFirstException | See Cancel(Boolean) for the effects of the parameter. |
Remarks
See Cancel(Boolean) for the exception behavior of this overload.
CanExecute()
Determines whether this AsyncRelayCommandCommon can execute.
Declaration
public bool CanExecute()
Returns
| Type | Description |
|---|---|
| Boolean |
|
ExecuteAsync()
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public async Task ExecuteAsync()
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
ExecuteAsync(CancellationToken)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public async Task ExecuteAsync(CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | An instance of CancellationToken to cancel the executing command delegate. |
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Only one command is executed at time. Sucessive command invocations are enqueued and run after the currently executing delegate has completed
or was cancelled. Query HasPending to know if the command has pending delegate executions and PendingCount to knwo the number of pending delegate executions.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If executing command delegate was cancelled. |
ExecuteAsync(TimeSpan)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public async Task ExecuteAsync(TimeSpan timeout)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeout | A TimeSpan to specify the timeout of the operation.
|
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Only one command is executed at time. Sucessive command invocations are enqueued and run after the currently executing delegate has completed
or was cancelled. Query HasPending to know if the command has pending delegate executions and PendingCount to knwo the number of pending delegate executions.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If the executing command delegate was cancelled. |
ExecuteAsync(TimeSpan, CancellationToken)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public virtual async Task ExecuteAsync(TimeSpan timeout, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeout | A TimeSpan to specify the timeout of the operation.
|
| CancellationToken | cancellationToken | An instance of CancellationToken to cancel the executing command delegate. |
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If the executing command delegate was cancelled. |
| ArgumentOutOfRangeException |
|
InvalidateCommand()
Raises the CanExecuteChanged event of this particular command only.
Declaration
public void InvalidateCommand()
OnCanExecuteChanged()
Raises the CanExecuteChanged event.
Declaration
protected virtual void OnCanExecuteChanged()
OnPropertyChanged(String)
Raises the PropertyChanged event.
Declaration
protected virtual void OnPropertyChanged(string propertyName = "")
Parameters
| Type | Name | Description |
|---|---|---|
| String | propertyName |
TryExecuteNoParamCommand()
Declaration
protected async Task<bool> TryExecuteNoParamCommand()
Returns
| Type | Description |
|---|---|
| Task<Boolean> |
Events
| Improve this Doc View SourceCanExecuteChanged
Declaration
public event EventHandler CanExecuteChanged
Event Type
| Type | Description |
|---|---|
| EventHandler |
PropertyChanged
Declaration
public event PropertyChangedEventHandler PropertyChanged
Event Type
| Type | Description |
|---|---|
| PropertyChangedEventHandler |
Explicit Interface Implementations
| Improve this Doc View SourceICommand.CanExecute(Object)
Declaration
bool ICommand.CanExecute(object parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | parameter |
Returns
| Type | Description |
|---|---|
| Boolean |
ICommand.Execute(Object)
Declaration
async void ICommand.Execute(object parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | parameter |