# Building advanced MVVM commands, Part 1

DevFeed: [Building advanced MVVM commands, Part 1](<https://devfeed.tech/articles/building-advanced-mvvm-commands-part-1-38403.md>)

Original publisher: [Read original article](<https://khmylov.com/2010/11/building-advanced-mvvm-commands-part-1/>)

Author: Andrew Khmylov

Published: 2010-11-24T00:00:00Z

Content type: tutorial

Language: en

Sources: [Despite the odds](<https://devfeed.tech/sources/despite-the-odds.md>)

Topics: [MVVM](<https://devfeed.tech/topics/mvvm.md>), [WPF](<https://devfeed.tech/topics/wpf.md>), [async](<https://devfeed.tech/topics/async.md>), [cancellation](<https://devfeed.tech/topics/cancellation.md>), [Exception](<https://devfeed.tech/topics/exception.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [applications](<https://devfeed.tech/tags/applications.md>), [async](<https://devfeed.tech/tags/async.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [cancellation](<https://devfeed.tech/tags/cancellation.md>), [exception-handling](<https://devfeed.tech/tags/exception-handling.md>), [mvvm](<https://devfeed.tech/tags/mvvm.md>), [thread](<https://devfeed.tech/tags/thread.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This tutorial presents an advanced DelegateCommand implementation for WPF and Silverlight applications using the MVVM pattern. It covers asynchronous execution, command states, cancellation, progress tracking, exception handling, UI updates through Dispatcher, and helper support for property-change notifications.

## Source excerpt

If you follow the MVVM pattern while developing your WPF/Silverlight applications, then you are probably familiar with DelegateCommand (or RelayCommand, as it is called sometimes) model. In brief: it is an implementation of WPF/Silverlight ICommand that allows you to specify what the command should do by passing some delegate that performs execution logic (instead of using CommandBindings). Pretty simple, but powerful pattern. Dealing with MVVM a lot, I've build my own implementation of this pattern that lets you simplify its usage. Here are some common features, that you have to handle on your own by using classic DelegateCommand: Support for asynchronous execution (usually solved by generating additional code in ViewModel that handles async calls and free/busy indicator). Different command states (Free/Busy/Failed with exception) + UI reactions for those states. Support for execution cancellation and progress tracking (required for asynchronous commands) Before we start, I offer you to imagine a common command execution timeline. In my opinion it looks like: Initialize some data (generally, UI settings - clear collections, set some indicators,...) Perform main execution logic (can be synchronous or asynchronous) Perform some actions after execution (update UI based on execution result) And don't forget about exception handling and correct cross-thread calls for asynchronous commands (all UI changes have to be performed through Dispatcher). Let's start with a little helper class, that I called PropertyChangedNotifier. It's just a wrapper around IPropertyChangedNotifier that has OnPropertyChanged(string propertyName) method. This method raises underlying IPropertyChangedNotifier.PropertyChanged event. Also this class has VerifyPropertyName method that verifies if specified string corresponds to the actual property (this method is marked with [Conditional("DEBUG")] attribute and is only used while debugging your applications to find out possible bugs with invalid prope