TPL——取消一个Task
2016-08-29 17:05
288 查看
取消一个Task
监控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 source
CancellationTokenSource tokenSource
= new CancellationTokenSource();
// create the cancellation token
CancellationToken token = tokenSource.Token;
// create the task
Task 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 delegate
token.Register(() => {
Console.WriteLine(">>>>>> Delegate Invoked\n");
});
// wait for input before we start the task
Console.WriteLine("Press enter to start task");
Console.WriteLine("Press enter again to cancel task");
Console.ReadLine();
// start the task
task.Start();
// read a line from the console.
Console.ReadLine();
// cancel the task
Console.WriteLine("Cancelling task");
tokenSource.Cancel();
// wait for input before exiting
Console.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 source
CancellationTokenSource tokenSource
= new CancellationTokenSource();
// create the cancellation token
CancellationToken token = tokenSource.Token;
// create the task
Task 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 handle
Task task2 = new Task(() => {
// wait on the handle
token.WaitHandle.WaitOne();
// write out a message
Console.WriteLine(">>>>> Wait handle released");
});
// wait for input before we start the task
Console.WriteLine("Press enter to start task");
Console.WriteLine("Press enter again to cancel task");
Console.ReadLine();
// start the tasks
task1.Start();
task2.Start();
// read a line from the console.
Console.ReadLine();
// cancel the task
Console.WriteLine("Cancelling task");
tokenSource.Cancel();
// wait for input before exiting
Console.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 source
CancellationTokenSource tokenSource
= new CancellationTokenSource();
// create the cancellation token
CancellationToken token = tokenSource.Token;
// create the tasks
Task 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 tasks
Console.WriteLine("Press enter to start tasks");
Console.WriteLine("Press enter again to cancel tasks");
Console.ReadLine();
// start the tasks
task1.Start();
task2.Start();
// read a line from the console.
Console.ReadLine();
// cancel the task
Console.WriteLine("Cancelling tasks");
tokenSource.Cancel();
// wait for input before exiting
Console.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 sources
CancellationTokenSource tokenSource1 = new CancellationTokenSource();
CancellationTokenSource tokenSource2 = new CancellationTokenSource();
CancellationTokenSource tokenSource3 = new CancellationTokenSource();
// create a composite token source using multiple tokens
CancellationTokenSource compositeSource =
CancellationTokenSource.CreateLinkedTokenSource(
tokenSource1.Token, tokenSource2.Token, tokenSource3.Token);
// create a cancellable task using the composite token
Task task = new Task(() => {
// wait until the token has been cancelled
compositeSource.Token.WaitHandle.WaitOne();
// throw a cancellation exception
throw new OperationCanceledException(compositeSource.Token);
}, compositeSource.Token);
// start the task
task.Start();
// cancel one of the original tokens
tokenSource2.Cancel();
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
}
}一个Task是否已经被取消?
You can determine if a
Task
has been cancelled by checking the
IsCancelled
property, which will return true
if the
Task was cancelled.
如果一个Task已经被取消,IsCancelled属性将返回true.
using System; using System.Threading; using System.Threading.Tasks; namespace Listing_07 { class Listing_07 { static void Main(string[] args) { // create the cancellation token source CancellationTokenSource tokenSource = new CancellationTokenSource(); // create the cancellation token CancellationToken token = tokenSource.Token; // create the task Task 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 task Console.WriteLine("Press enter to start task"); Console.WriteLine("Press enter again to cancel task"); Console.ReadLine(); // start the task task.Start(); // read a line from the console. Console.ReadLine(); // cancel the task Console.WriteLine("Cancelling task"); tokenSource.Cancel(); // wait for input before exiting Console.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 source
CancellationTokenSource tokenSource
= new CancellationTokenSource();
// create the cancellation token
CancellationToken token = tokenSource.Token;
// create the task
Task 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 delegate
token.Register(() => {
Console.WriteLine(">>>>>> Delegate Invoked\n");
});
// wait for input before we start the task
Console.WriteLine("Press enter to start task");
Console.WriteLine("Press enter again to cancel task");
Console.ReadLine();
// start the task
task.Start();
// read a line from the console.
Console.ReadLine();
// cancel the task
Console.WriteLine("Cancelling task");
tokenSource.Cancel();
// wait for input before exiting
Console.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 source
CancellationTokenSource tokenSource
= new CancellationTokenSource();
// create the cancellation token
CancellationToken token = tokenSource.Token;
// create the task
Task 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 handle
Task task2 = new Task(() => {
// wait on the handle
token.WaitHandle.WaitOne();
// write out a message
Console.WriteLine(">>>>> Wait handle released");
});
// wait for input before we start the task
Console.WriteLine("Press enter to start task");
Console.WriteLine("Press enter again to cancel task");
Console.ReadLine();
// start the tasks
task1.Start();
task2.Start();
// read a line from the console.
Console.ReadLine();
// cancel the task
Console.WriteLine("Cancelling task");
tokenSource.Cancel();
// wait for input before exiting
Console.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 source
CancellationTokenSource tokenSource
= new CancellationTokenSource();
// create the cancellation token
CancellationToken token = tokenSource.Token;
// create the tasks
Task 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 tasks
Console.WriteLine("Press enter to start tasks");
Console.WriteLine("Press enter again to cancel tasks");
Console.ReadLine();
// start the tasks
task1.Start();
task2.Start();
// read a line from the console.
Console.ReadLine();
// cancel the task
Console.WriteLine("Cancelling tasks");
tokenSource.Cancel();
// wait for input before exiting
Console.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 sources
CancellationTokenSource tokenSource1 = new CancellationTokenSource();
CancellationTokenSource tokenSource2 = new CancellationTokenSource();
CancellationTokenSource tokenSource3 = new CancellationTokenSource();
// create a composite token source using multiple tokens
CancellationTokenSource compositeSource =
CancellationTokenSource.CreateLinkedTokenSource(
tokenSource1.Token, tokenSource2.Token, tokenSource3.Token);
// create a cancellable task using the composite token
Task task = new Task(() => {
// wait until the token has been cancelled
compositeSource.Token.WaitHandle.WaitOne();
// throw a cancellation exception
throw new OperationCanceledException(compositeSource.Token);
}, compositeSource.Token);
// start the task
task.Start();
// cancel one of the original tokens
tokenSource2.Cancel();
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
}
}一个Task是否已经被取消?
You can determine if a
Task
has been cancelled by checking the
IsCancelled
property, which will return true
if the
Task was cancelled.
如果一个Task已经被取消,IsCancelled属性将返回true.
相关文章推荐
- TPL——开始一个Task
- System.Threading.Tasks.TaskCanceledException: 已取消一个任务
- c# Task编程一个task抛出异常后怎么取消其他线程
- python用法: post一个http请求, schedule一个task
- 取消关机的一个小技巧
- 当一个公司抠门到连年中聚会都取消的时候, 还能指望什么?
- vi一个文件时如何取消匹配字的颜色显示
- 怎么用JS显示一个确认取消对话提示?
- 要正确删除一个策略,应当首先取消指定,然后再删除
- TPL: 一个新的C++正则表达式(regex)库
- 取消一个正在执行的异步回发
- BBS 设计思路系列 ---- 为了推实名制,一个用户一个账号??取消奖励转移??
- TPL: 一个新的正则表达式(regex)库
- window.confirm 参数就只有一个。显示提示框的信息。按确定,返回true;按取消返回false。
- SDC task library -- 一个MSBuild Task的有益补充。
- python用法: post一个http请求, schedule一个task
- 编写一个脚本取消Windows激活通知气球
- 一个叫选择所有,一个叫取消所有选择
- TimerTask在遇到修改系统时间不能正常工作,自己写一个简单的TimerTask和Timer
- 在删除数据库记录前弹出一个对话框,和确认按钮和取消按钮