在.Net和C#中運行異步代碼至關簡單,由於咱們有時候須要取消正在進行的異步操做,經過本文,能夠掌握 經過CancellationToken取消任務(包括non-cancellable任務)。html
private void BackgroundLongRunningTask(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; for (int i = 1; i <= 10000; i++) { if (worker.CancellationPending == true) { e.Cancel = true; break; } // Do something } }
已經再也不推薦這種方式來完成異步和長時間運行的操做,可是大部分概念在如今依舊可使用。git
/// <summary> /// Compute a value for a long time. /// </summary> /// <returns>The value computed.</returns> /// <param name="loop">Number of iterations to do.</param> private static Task<decimal> LongRunningOperation(int loop) { // Start a task and return it return Task.Run(() => { decimal result = 0; // Loop for a defined number of iterations for (int i = 0; i < loop; i++) { // Do something that takes times like a Thread.Sleep in .NET Core 2. Thread.Sleep(10); result += i; } return result; }); } // 這裏咱們使用Thread.Sleep 模仿長時間運行的操做
簡單異步調用代碼:github
public static async Task ExecuteTaskAsync() { Console.WriteLine(nameof(ExecuteTaskAsync)); Console.WriteLine("Result {0}", await LongRunningOperation(100)); Console.WriteLine("Press enter to continue"); Console.ReadLine(); }
/// <summary> /// Compute a value for a long time. /// </summary> /// <returns>The value computed.</returns> /// <param name="loop">Number of iterations to do.</param> /// <param name="cancellationToken">The cancellation token.</param> private static Task<decimal> LongRunningCancellableOperation(int loop, CancellationToken cancellationToken) { Task<decimal> task = null; // Start a task and return it task = Task.Run(() => { decimal result = 0; // Loop for a defined number of iterations for (int i = 0; i < loop; i++) { // Check if a cancellation is requested, if yes, // throw a TaskCanceledException. if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(task); // Do something that takes times like a Thread.Sleep in .NET Core 2. Thread.Sleep(10); result += i; } return result; }); return task; }
// 如下代碼 利用 CancellationSource默認構造函數 完成超時取消 public static async Task ExecuteTaskWithTimeoutAsync(TimeSpan timeSpan) { Console.WriteLine(nameof(ExecuteTaskWithTimeoutAsync)); using (var cancellationTokenSource = new CancellationTokenSource(timeSpan)) { try { var result = await LongRunningCancellableOperation(500, cancellationTokenSource.Token); Console.WriteLine("Result {0}", result); } catch (TaskCanceledException) { Console.WriteLine("Task was cancelled"); } } Console.WriteLine("Press enter to continue"); Console.ReadLine(); }
public static async Task ExecuteManuallyCancellableTaskAsync() { Console.WriteLine(nameof(ExecuteManuallyCancellableTaskAsync)); using (var cancellationTokenSource = new CancellationTokenSource()) { // Creating a task to listen to keyboard key press var keyBoardTask = Task.Run(() => { Console.WriteLine("Press enter to cancel"); Console.ReadKey(); // Cancel the task cancellationTokenSource.Cancel(); }); try { var longRunningTask = LongRunningCancellableOperation(500, cancellationTokenSource.Token); var result = await longRunningTask; Console.WriteLine("Result {0}", result); Console.WriteLine("Press enter to continue"); } catch (TaskCanceledException) { Console.WriteLine("Task was cancelled"); } await keyBoardTask; } }
// 以上是一個控制檯程序,異步接收控制檯輸入,發出取消命令。
private static async Task<decimal> LongRunningOperationWithCancellationTokenAsync(int loop, CancellationToken cancellationToken) { // We create a TaskCompletionSource of decimal var taskCompletionSource = new TaskCompletionSource<decimal>(); // Registering a lambda into the cancellationToken cancellationToken.Register(() => { // We received a cancellation message, cancel the TaskCompletionSource.Task taskCompletionSource.TrySetCanceled(); }); var task = LongRunningOperation(loop); // Wait for the first task to finish among the two var completedTask = await Task.WhenAny(task, taskCompletionSource.Task); return await completedTask; }
像上面代碼同樣執行取消命令 :異步
public static async Task CancelANonCancellableTaskAsync() { Console.WriteLine(nameof(CancelANonCancellableTaskAsync)); using (var cancellationTokenSource = new CancellationTokenSource()) { // Listening to key press to cancel var keyBoardTask = Task.Run(() => { Console.WriteLine("Press enter to cancel"); Console.ReadKey(); // Sending the cancellation message cancellationTokenSource.Cancel(); }); try { // Running the long running task var longRunningTask = LongRunningOperationWithCancellationTokenAsync(100, cancellationTokenSource.Token); var result = await longRunningTask; Console.WriteLine("Result {0}", result); Console.WriteLine("Press enter to continue"); } catch (TaskCanceledException) { Console.WriteLine("Task was cancelled"); } await keyBoardTask; } }