TPL——取消一个Task

来源:互联网 发布:科学引文索引数据库 编辑:程序博客网 时间:2024/06/01 09:29

取消一个Task

using System;using System.Threading;using System.Threading.Tasks;namespace Listing_07 {class Listing_07 {static void Main(string[] args) {// create the cancellation token sourceCancellationTokenSource tokenSource= new CancellationTokenSource();// create the cancellation tokenCancellationToken token = tokenSource.Token;// create the taskTask task = new Task(() => {for (int i = 0; i < int.MaxValue; i++) {if (token.IsCancellationRequested) {Console.WriteLine("Task cancel detected");throw new OperationCanceledException(token);} else {Console.WriteLine("Int value {0}", i);}}}, token);// wait for input before we start the taskConsole.WriteLine("Press enter to start task");Console.WriteLine("Press enter again to cancel task");Console.ReadLine();// start the tasktask.Start();// read a line from the console.Console.ReadLine();// cancel the taskConsole.WriteLine("Cancelling task");tokenSource.Cancel();// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}}}

监控Task的取消1:使用委托

using System;using System.Threading;using System.Threading.Tasks;namespace Listing_08 {class Listing_08 {static void Main(string[] args) {// create the cancellation token sourceCancellationTokenSource tokenSource= new CancellationTokenSource();// create the cancellation tokenCancellationToken token = tokenSource.Token;// create the taskTask task = new Task(() => {for (int i = 0; i < int.MaxValue; i++) {if (token.IsCancellationRequested) {Console.WriteLine("Task cancel detected");throw new OperationCanceledException(token);} else {Console.WriteLine("Int value {0}", i);}}}, token);// register a cancellation delegatetoken.Register(() => {Console.WriteLine(">>>>>> Delegate Invoked\n");});// wait for input before we start the taskConsole.WriteLine("Press enter to start task");Console.WriteLine("Press enter again to cancel task");Console.ReadLine();// start the tasktask.Start();// read a line from the console.Console.ReadLine();// cancel the taskConsole.WriteLine("Cancelling task");tokenSource.Cancel();// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}}}
监控Task的取消2:WaitOne()

using System;using System.Threading;using System.Threading.Tasks;namespace Listing_09 {class Listing_09 {static void Main(string[] args) {// create the cancellation token sourceCancellationTokenSource tokenSource= new CancellationTokenSource();// create the cancellation tokenCancellationToken token = tokenSource.Token;// create the taskTask task1 = new Task(() => {for (int i = 0; i < int.MaxValue; i++) {if (token.IsCancellationRequested) {Console.WriteLine("Task cancel detected");throw new OperationCanceledException(token);} else {Console.WriteLine("Int value {0}", i);}}}, token);// create a second task that will use the wait handleTask task2 = new Task(() => {// wait on the handletoken.WaitHandle.WaitOne();// write out a messageConsole.WriteLine(">>>>> Wait handle released");});// wait for input before we start the taskConsole.WriteLine("Press enter to start task");Console.WriteLine("Press enter again to cancel task");Console.ReadLine();// start the taskstask1.Start();task2.Start();// read a line from the console.Console.ReadLine();// cancel the taskConsole.WriteLine("Cancelling task");tokenSource.Cancel();// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}}}
取消几个Task

using System;using System.Threading;using System.Threading.Tasks;namespace Listing_10 {class Listing_10 {static void Main(string[] args) {// create the cancellation token sourceCancellationTokenSource tokenSource= new CancellationTokenSource();// create the cancellation tokenCancellationToken token = tokenSource.Token;// create the tasksTask task1 = new Task(() => {for (int i = 0; i < int.MaxValue; i++) {token.ThrowIfCancellationRequested();Console.WriteLine("Task 1 - Int value {0}", i);}}, token);Task task2 = new Task(() => {for (int i = 0; i < int.MaxValue; i++) {token.ThrowIfCancellationRequested();Console.WriteLine("Task 2 - Int value {0}", i);}}, token);// wait for input before we start the tasksConsole.WriteLine("Press enter to start tasks");Console.WriteLine("Press enter again to cancel tasks");Console.ReadLine();// start the taskstask1.Start();task2.Start();// read a line from the console.Console.ReadLine();// cancel the taskConsole.WriteLine("Cancelling tasks");tokenSource.Cancel();// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}}}
复合Token

using System;using System.Threading;using System.Threading.Tasks;namespace Listing_11 {class Listing_11 {static void Main(string[] args) {// create the cancellation token sourcesCancellationTokenSource tokenSource1 = new CancellationTokenSource();CancellationTokenSource tokenSource2 = new CancellationTokenSource();CancellationTokenSource tokenSource3 = new CancellationTokenSource();// create a composite token source using multiple tokensCancellationTokenSource compositeSource =CancellationTokenSource.CreateLinkedTokenSource(tokenSource1.Token, tokenSource2.Token, tokenSource3.Token);// create a cancellable task using the composite tokenTask task = new Task(() => {// wait until the token has been cancelledcompositeSource.Token.WaitHandle.WaitOne();// throw a cancellation exceptionthrow new OperationCanceledException(compositeSource.Token);}, compositeSource.Token);// start the tasktask.Start();// cancel one of the original tokenstokenSource2.Cancel();// wait for input before exitingConsole.WriteLine("Main method complete. Press enter to finish.");Console.ReadLine();}}}
一个Task是否已经被取消?

You can determine if aTask has been cancelled by checking theIsCancelledproperty, which will return trueif the Task was cancelled.

如果一个Task已经被取消,IsCancelled属性将返回true.






0 0