CancellationAsyncDisposable.cs 896 B

123456789101112131415161718192021222324252627282930313233
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. _cts = cts ?? throw new ArgumentNullException(nameof(cts));
  18. }
  19. public CancellationToken Token => _cts.Token;
  20. public ValueTask DisposeAsync()
  21. {
  22. _cts.Cancel();
  23. return default;
  24. }
  25. }
  26. }