CancellationAsyncDisposable.cs 953 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive.Disposables
  7. {
  8. public sealed class CancellationAsyncDisposable : IAsyncDisposable
  9. {
  10. private readonly CancellationTokenSource _cts;
  11. public CancellationAsyncDisposable()
  12. : this(new CancellationTokenSource())
  13. {
  14. }
  15. public CancellationAsyncDisposable(CancellationTokenSource cts)
  16. {
  17. if (cts == null)
  18. throw new ArgumentNullException(nameof(cts));
  19. _cts = cts;
  20. }
  21. public CancellationToken Token => _cts.Token;
  22. public Task DisposeAsync()
  23. {
  24. _cts.Cancel();
  25. return Task.CompletedTask;
  26. }
  27. }
  28. }