[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
  Prerequisites
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]View Models
  [Collapse]Commands
    Delegate Commands
    Asynchronous Commands
  [Expand]Behaviors
  [Expand]Services
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

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. 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.

POCO ViewModels and ViewModelBase descendants can automatically generate asynchronous commands for methods marked with the async keyword.

You can reference your asynchronous method when invalidating an auto-generated asynchronous command:

When a command with a parameter is used, you can specify any value. It will not affect the result:

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 as follows.

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

Expanded AsyncCommands and IDispatcherService

If you need to access the execution process from the main thread, use the IDispatcherService as follows.

Expanded AsyncCommands in POCO

POCO ViewModels can 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?​​​​​​​