Skip to content

Question: Cancellation #58

Closed
Closed

Description

@ModernRonin

Hi,

first of all thanks for a very useful library :-)

My question is: how would I make an AsyncCommand cancellable? Scenario: cmd is bound to a long running operation, say a big download, that the user should be able to cancel via another command.

I can't seem to find any support for cancellation, but maybe I'm overlooking something?

Cheers & thanks,
MR

Activity

  1. johnthiriet commented on Mar 3, 2021

    @johnthiriet

    Hello,

    This might not be the best place to ask a question as it is not an issue per say but here is what you can do in pseudo code.

    ViewModel {
      CancellationTokenSource _cts;
      AsyncCommand StartCommand(async () => {
       try  {
         _cts?.Cancel();
        _cts?.Dispose();
        _cts = new CancellationTokenSource();
        StartYourProcessThatHasCancellationAsync(_cts.Token);
       }
       finally {
         _cts?.Dispose();
        _cts = null;
       }
      }
      
      AsyncCommand StopCommand(() => {
         _cts?.Cancel();
      });
    }
    
  2. Repository owner locked and limited conversation to collaborators on Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions