Log In
[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
 [Expand]What's Installed
 [Expand]Common Concepts
 [Collapse]MVVM Framework
    Delegate Commands
    Asynchronous Commands
   Data Annotation Attributes
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Asynchronous Commands

Asynchronous commands can be useful if you need to run a time-consuming operation in a separate thread without freezing the UI. For instance, if you need to calculate something, you can bind a button to an asynchronous command. When an end-user clicks the button, the command starts the calculation process and the button becomes disabled. When the process is done, the button is enabled.

The following asynchronous commands are available.

  • AsyncCommand<T> - Specifies a command whose Execute and CanExecute delegates accept a single parameter of type T.
  • AsyncCommand - Specifies a command whose Execute and CanExecute delegates do not have any parameters.

Expanded Creating AsyncCommands

The AsyncCommand and AsyncCommand<T> can be created similarly to the regular Delegate Commands. There are two constructors: a constructor that accepts the Execute delegate, and a second constructor that accepts Execute and CanExecute delegates. The Execute delegate should return a Task object. Similar to Delegate Commands, the AsyncCommand constructors support the optional useCommandManager parameter (which equals True by default) that specifies whether the CommandManager is used to raise the CanExecuteChanged event.

Expanded Running AsyncCommands

You can bind to AsyncCommands in the same manner as any ICommand.

Expanded Preventing simultaneous execution

The AsyncCommand and AsyncCommand<T> classes provide the IsExecuting property. While the command execution task is working, this property equals True and the AsyncCommand.CanExecute method always returns False, no matter what you implemented in the CanExecute delegate. This feature allows you to disable a control bound to the command until the previous command execution is completed.

You can disable this behavior by setting the AsyncCommand.AllowMultipleExecution property to True. In this case, the AsyncCommand.CanExecute method returns a value based on your CanExecute delegate implementation.

Expanded Canceling command executions

The AsynCommands provide the IsCancellationRequested property, which you can check in the execution method to implement canceling the command execution.

The AsyncCommand.IsCancellationRequested property is set to True when AsyncCommand.CancelCommand is invoked. You can bind a control to the CancelCommand in the following manner.

If you need more control of the cancellation process, use the AsyncCommand.CancellationTokenSource property. For instance:

Expanded AsyncCommands and IDispatcherService

If it is required that you report progress (update the UI) from the execution process to the main thread, use the IDispatcherService as follows.

Expanded AsyncCommands in POCO

POCO provides the capability to automatically create AsyncCommands based on a public function returning a Task object (the function should have one parameter or be parameterless). To access the IsExecuting and IsCancellationRequested command properties, you can use the DevExpress.Mvvm.POCO.POCOViewModelExtensions class, which provides the GetAsyncCommand method. Below is an example of a POCO ViewModel that creates an async CalculateCommand for the Calculate method.

Is this topic helpful?​​​​​​​