diff --git a/PolyShim.Tests/Net80/CancellationTokenSourceTests.cs b/PolyShim.Tests/Net80/CancellationTokenSourceTests.cs new file mode 100644 index 0000000..f6883e7 --- /dev/null +++ b/PolyShim.Tests/Net80/CancellationTokenSourceTests.cs @@ -0,0 +1,22 @@ +using System.Threading; +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace PolyShim.Tests.Net80; + +public class CancellationTokenSourceTests +{ + [Fact] + public async Task CancelAsync_Test() + { + // Arrange + using var cts = new CancellationTokenSource(); + + // Act + await cts.CancelAsync(); + + // Assert + cts.IsCancellationRequested.Should().BeTrue(); + } +} diff --git a/PolyShim/Net80/CancellationTokenSource.cs b/PolyShim/Net80/CancellationTokenSource.cs new file mode 100644 index 0000000..45205fe --- /dev/null +++ b/PolyShim/Net80/CancellationTokenSource.cs @@ -0,0 +1,30 @@ +#if FEATURE_TASK +#if (NETCOREAPP && !NET8_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) +#nullable enable +// ReSharper disable RedundantUsingDirective +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable PartialTypeWithSinglePart + +using System.Collections; +using System.Threading; +using System.Threading.Tasks; + +internal static partial class PolyfillExtensions +{ + // https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.cancelasync + public static Task CancelAsync(this CancellationTokenSource cts) + { +#if (NETFRAMEWORK && !NET45_OR_GREATER) + return Task.Factory.StartNew(() => cts.Cancel()); +#elif (NETFRAMEWORK && !NET46_OR_GREATER) || (NETSTANDARD && !NETSTANDARD1_3_OR_GREATER) + cts.Cancel(); + return Task.FromResult(null); +#else + cts.Cancel(); + return Task.CompletedTask; +#endif + } +} +#endif +#endif