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
- TPL——取消一个Task
- TPL——开始一个Task
- TPL——等待Task
- TPL——Task的状态及堕性Task
- 【C#】43. TPL基础——Task初步
- Task Parallel Library ( TPL)学习
- TPL Part 1 Task 的使用
- TPL Part 4 -- Task的协同
- 11-14的代码子,for tpl variable——modifier。tpl 你们懂得~
- TPL
- 微软最新的线程编程方式TPL(Task Paraller Library)
- C# 异步编程3 TPL Task 异步程序开发
- C# 异步编程3 TPL Task 异步程序开发
- 一个C的序列化库tpl
- 使用CancellationTokenSource来取消Task
- 一个TASK
- linux程序设计——取消一个线程(第十二章)
- .NET(C#) TPL:Task中未觉察异常和TaskScheduler.UnobservedTaskException事件
- [转]【坐在马桶上看算法】算法2:邻居好说话:冒泡排序--作者:ahalei
- iOS-RAC学习笔记(二)——RACSignal订阅
- Spring框架介绍
- 反射の从Class建立对象
- 终于推开那扇门
- TPL——取消一个Task
- 微信公众平台jsapi开发教程(1)获取jsapi_ticket
- 美化 checkbox
- iOS图片拉伸技巧
- 苹果微信版语音播放支持mp3安卓微信语音播放支持的是ogg
- Universal-Image-Loader源码阅读(29)-SimpleBitmapDisplayer
- 【入门教程之七】 玩转源代码:生成工程解决方案与源码编译
- 算法的基本概念及特性(有穷性、确定性、可行性、输入和输出)
- LeetCode : 136. Single Number