您的位置:首页 > 编程语言 > PHP开发

TPL——取消一个Task

2016-08-29 17:05 288 查看
取消一个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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: