Mike-Ward.Net

AsyncCommand Implementation in WPF

I found this nifty implementation of a asynchronous command using the new async/await syntax in C# 4.5 in some code I was reviewing. Nice abstraction.

using System;  
using System.Threading.Tasks;  
using System.Windows.Input;  
  
namespace OpenWeather.Command  
{  
    internal class AsyncCommand : ICommand  
    {  
        private readonly Func<Task> _execute;  
        private readonly Func<bool> _canExecute;  
        private bool _isExecuting;  
  
        public AsyncCommand(Func<Task> execute) : this(execute, () => true)  
        {  
        }  
  
        public AsyncCommand(Func<Task> execute, Func<bool> canExecute)  
        {  
            _execute = execute;  
            _canExecute = canExecute;  
        }  
  
        public bool CanExecute(object parameter)  
        {  
            return !(_isExecuting && _canExecute());  
        }  
  
        public event EventHandler CanExecuteChanged;  
  
        public async void Execute(object parameter)  
        {  
            _isExecuting = true;  
            OnCanExecuteChanged();  
            try  
            {  
                await _execute();  
            }  
            finally  
            {  
                _isExecuting = false;  
                OnCanExecuteChanged();  
            }  
        }  
  
        protected virtual void OnCanExecuteChanged()  
        {  
            if (CanExecuteChanged != null) CanExecuteChanged(this, new EventArgs());  
        }  
    }  
}

Original article and license: http://www.codeproject.com/Articles/630248/WPF-OpenWeather

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Quick Tip - Clear ReSharper's Cache to Fix False Errors
Quick Tip - Refined GitHub
Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
More... (1077)

Donate with PayPal