Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
Forked from rossdargan/AsyncCommand
Last active May 3, 2016 22:43
Show Gist options
  • Select an option

  • Save NickStrupat/939cf839b32fb0e36eba to your computer and use it in GitHub Desktop.

Select an option

Save NickStrupat/939cf839b32fb0e36eba to your computer and use it in GitHub Desktop.

Revisions

  1. NickStrupat revised this gist May 3, 2016. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions AsyncCommand
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,17 @@ using System.Threading.Tasks;
    using System.Windows.Input;

    class AsyncCommand : ICommand {
    public Boolean CanExecute(Object parameter = null) => !isRunning;
    public Boolean CanExecute(Object parameter = null) => !IsRunning;

    public event EventHandler CanExecuteChanged = delegate { };

    public async void Execute(Object parameter = null) {
    isRunning = true;
    IsRunning = true;
    try {
    await asyncAction();
    }
    finally {
    isRunning = false;
    IsRunning = false;
    }
    }

    @@ -23,13 +23,13 @@ class AsyncCommand : ICommand {
    this.asyncAction = asyncAction;
    }

    private Boolean isRunningField;
    private Boolean isRunning {
    private Boolean isRunning;
    private Boolean IsRunning {
    get {
    return isRunningField;
    return isRunning;
    }
    set {
    isRunningField = value;
    isRunning = value;
    CanExecuteChanged.Invoke(this, EventArgs.Empty);
    }
    }
  2. NickStrupat revised this gist May 3, 2016. 1 changed file with 31 additions and 32 deletions.
    63 changes: 31 additions & 32 deletions AsyncCommand
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,36 @@
    class AsyncCommand : ICommand
    {
    private readonly Func<Task> asyncAction;
    private bool isRunning = false;
    using System;
    using System.Threading.Tasks;
    using System.Windows.Input;

    public bool IsRunning
    {
    get { return isRunning; }
    set
    {
    isRunning = value;
    if (CanExecuteChanged != null)
    {
    CanExecuteChanged(this, EventArgs.Empty);
    }
    }
    }
    class AsyncCommand : ICommand {
    public Boolean CanExecute(Object parameter = null) => !isRunning;

    public AsyncCommand(Func<Task> asyncAction)
    {
    this.asyncAction = asyncAction;
    }
    public event EventHandler CanExecuteChanged = delegate { };

    public bool CanExecute(object parameter)
    {
    return !isRunning;
    }
    public async void Execute(Object parameter = null) {
    isRunning = true;
    try {
    await asyncAction();
    }
    finally {
    isRunning = false;
    }
    }

    public event EventHandler CanExecuteChanged;
    private readonly Func<Task> asyncAction;

    public async void Execute(object parameter)
    {
    IsRunning = true;
    await asyncAction();
    IsRunning = false;
    }
    }
    public AsyncCommand(Func<Task> asyncAction) {
    this.asyncAction = asyncAction;
    }

    private Boolean isRunningField;
    private Boolean isRunning {
    get {
    return isRunningField;
    }
    set {
    isRunningField = value;
    CanExecuteChanged.Invoke(this, EventArgs.Empty);
    }
    }
    }
  3. @rossdargan rossdargan created this gist Nov 17, 2014.
    37 changes: 37 additions & 0 deletions AsyncCommand
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    class AsyncCommand : ICommand
    {
    private readonly Func<Task> asyncAction;
    private bool isRunning = false;

    public bool IsRunning
    {
    get { return isRunning; }
    set
    {
    isRunning = value;
    if (CanExecuteChanged != null)
    {
    CanExecuteChanged(this, EventArgs.Empty);
    }
    }
    }

    public AsyncCommand(Func<Task> asyncAction)
    {
    this.asyncAction = asyncAction;
    }

    public bool CanExecute(object parameter)
    {
    return !isRunning;
    }

    public event EventHandler CanExecuteChanged;

    public async void Execute(object parameter)
    {
    IsRunning = true;
    await asyncAction();
    IsRunning = false;
    }
    }