Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
Forked from rossdargan/AsyncCommand
Last active May 3, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickStrupat/939cf839b32fb0e36eba to your computer and use it in GitHub Desktop.
Save NickStrupat/939cf839b32fb0e36eba to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using System.Windows.Input;
class AsyncCommand : ICommand {
public Boolean CanExecute(Object parameter = null) => !IsRunning;
public event EventHandler CanExecuteChanged = delegate { };
public async void Execute(Object parameter = null) {
IsRunning = true;
try {
await asyncAction();
}
finally {
IsRunning = false;
}
}
private readonly Func<Task> asyncAction;
public AsyncCommand(Func<Task> asyncAction) {
this.asyncAction = asyncAction;
}
private Boolean isRunning;
private Boolean IsRunning {
get {
return isRunning;
}
set {
isRunning = value;
CanExecuteChanged.Invoke(this, EventArgs.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment