Tutorial, Dotnet, Csharp, Programming, Coding.
i++) { cts.Token.ThrowIfCancellationRequested(); Console.WriteLine($"Working... {i}"); Thread.Sleep(1000); // Simulate work } }, cts.Token); Thread.Sleep(3000); // Let the task run for a bit cts.Cancel(); try { longRunningTask.Wait(); } catch (AggregateException ae) { Console.WriteLine("Task was