心清的专栏

C/C++、linux系统编程、matlab,关注机器学习,数据挖掘等领域

TPL——取消一个Task

取消一个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 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.






阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010155023/article/details/52354847
文章标签: C# TPL Task 多线程
个人分类: Threading in C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

TPL——取消一个Task

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭