Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a cancellationtoken or expose a method for cancellation?

Which one of the following two examples is preferred?

Example 1

public class Worker : IDisposable
{
    private CancellationTokenSource tokenSource;

    public string State { get; private set; }

    public async Task StartWorkAsync()
    {
        tokenSource = new CancellationTokenSource();

        this.State = "Working";
        await Task.Delay(5000, tokenSource.Token);
    }

    public void StopWork()
    {
        this.tokenSource.Cancel();
        this.State = "Stopped";
    }

    public void Dispose()
    {
        tokenSource?.Dispose();
    }
}

Example 2

public class Worker
{
    public string State { get; private set; }

    public async Task StartWorkAsync(CancellationToken ctoken)
    {
        ctoken.ThrowIfCancellationRequested();

        this.State = "Working";
        try
        {
            await Task.Delay(5000, ctoken);
        }
        catch (AggregateException) when (ctoken.IsCancellationRequested)
        {
            this.State = "Stopped";
        }
    }
}

Or perhaps just both? However, I assume that it's expect that it's common practice to take a cancellationtoken with an async method.

like image 769
Erik Bäckman Avatar asked Oct 18 '25 03:10

Erik Bäckman


1 Answers

You should accept a CancellationToken as an argument and allow the OperationCanceledException to propagate:

public async Task StartWorkAsync(CancellationToken ctoken)
{
  ctoken.ThrowIfCancellationRequested();

  this.State = "Working";
  try
  {
    await Task.Delay(5000, ctoken);
  }
  catch (OperationCanceledException)
  {
    this.State = "Stopped";
    throw;
  }
}
like image 169
Stephen Cleary Avatar answered Oct 19 '25 16:10

Stephen Cleary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!