TPL异步并行编程之简单使用
在上篇随笔里面说明了Task的使用,仅仅是简单使用,有时候把一个任务交给Task去执行,但是呢还是要管理下,比如说:我要叫这个任务停止了,不做了,任务取消了,或者超时了
在传统的While里面我们可以这样做,1 通过标识 2 通过一个方法抛异常,3 其他办法
举个例子:while(true){
if(isNotCancel){
//每次都判断下,取消没有,当然isNotCancel需要加上lock的
}
}
难道在Task里面有什么新奇吗?其实也没啥新奇的,那Task怎么取消呢?来点干货吧
一 轮训检测
直接调用task.cancel方法,但是下Task函数体内部必须做轮训检测是否被取消,且看代码
static void Main(string[] args)
{
CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; Task task=new Task(() =>
{
while (true)
{
if (token.IsCancellationRequested)
{
break;
}
else
{ }
}
},token); Console.WriteLine("运行"); task.Start(); Thread.Sleep(*); tokenSource.Cancel(); Console.ReadKey();
}
二 取消过程拦截检测
在调用task.cancel方法是会在真正改变token.IsCancellationRequested值之前,调用token中所注册的函数,也就是说token.cancel(),调用后会调用token.register所注册的方法,然后再更改token.IsCancellationRequested只为true,那么反过来在注册的方法中我们就可以检测是否调用了cancel方法了也就自然检测到了已经取消了
且看代码说话:
static void Main(string[] args)
{
CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; Task task=new Task(() =>
{
while (true)
{
if (token.IsCancellationRequested)
{
break;
}
else
{ }
}
},token); Console.WriteLine("运行"); token.Register(() =>
{
Console.WriteLine("取消了,在取消之前必定调用了我");
}); task.Start(); Thread.Sleep(*); tokenSource.Cancel(); Console.ReadKey();
}
三 使用信号量来检测是否取消
现在我们启用2个Task,TaskA,TaskB,TaskB需要TaskA取消后才能执行,那么我们也可以在TaskB中执行代码时检测TaskA已被取消了,且看代码
static void Main(string[] args)
{
CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; Task task=new Task(() =>
{
while (true)
{
// 一直在运行,下面那个家伙得等着我被取消或者把事情做完
if (token.IsCancellationRequested)
{
//我已被取消该时候退出了
break;
}
}
},token); Task.Run(() =>
{
//我一开始就被上面那个task家伙挂起了,我需要他取消我才能干活~~
token.WaitHandle.WaitOne();
while (true)
{
// 开始干活
}
}); task.Start(); Thread.Sleep(*); tokenSource.Cancel(); Console.ReadKey();
}
四 多个协作的Task一个取消则其他Task也被取消,这样也可以取消一组Task
就好比我们几个人一起干一件事情,但是这件事情需要每个分工的相互协作才能继续,比如玉女双休剑,需要2人同时练功才行,其中一个人说我不行了 那都不行了,且看代码
4.1 共用一个Token
static void Main(string[] args)
{
CancellationTokenSource tokenSource = new CancellationTokenSource(); // create the cancellation token
CancellationToken token = tokenSource.Token; // create the tasks
Task task1 = new Task(() =>
{
for (int i = ; i < int.MaxValue; i++)
{
token.ThrowIfCancellationRequested();
Console.WriteLine("Task 1 - Int value {0}", i);
}
}, token); Task task2 = new Task(() =>
{
for (int i = ; 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();
}
4.2 Token组
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
Thread.Sleep(*);
tokenSource2.Cancel(); // wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
五 抛出异常
ThrowIfCancellationRequested,在四中已经看到如果调用cancel方法会处罚ThrowIfCancellationRequested函数的执行,那么相应的Task检测到异常如果不做任何处理的情况下也就退出了,且看代码
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> static void Main(string[] args)
{
// create the cancellation token source
CancellationTokenSource tokenSource1 = new CancellationTokenSource(); // create the cancellation token
CancellationToken token1 = tokenSource1.Token; // create the first task, which we will let run fully
Task task1 = new Task(() =>
{
for (int i = ; i < ; i++)
{
token1.ThrowIfCancellationRequested();
Console.WriteLine("Task 1 - Int value {0}", i);
}
}, token1); // create the second cancellation token source
CancellationTokenSource tokenSource2 = new CancellationTokenSource(); // create the cancellation token
CancellationToken token2 = tokenSource2.Token; // create the second task, which we will cancel
Task task2 = new Task(() =>
{
for (int i = ; i < int.MaxValue; i++)
{
token2.ThrowIfCancellationRequested();
Console.WriteLine("Task 2 - Int value {0}", i);
}
}, token2); // start all of the tasks
task1.Start();
task2.Start(); // cancel the second token source
tokenSource2.Cancel();
// write out the cancellation detail of each task
Console.WriteLine("Task 1 cancelled? {0}", task1.IsCanceled);
Console.WriteLine("Task 2 cancelled? {0}", task2.IsCanceled);
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
最后
其实取消Task的执行还有其他办法,也可以自己实现不一定就要TPL通过的api来实现