心清的专栏

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

TPL——等待Task

等待特定时间:

CancallationToken.WaitHandle.WaitOne()
Thread.Sleep()
Thread.SpinWait()
前两个函数将在等待的时候被投入睡眠,而Thread.SpinWait()却会使线程处于一个高强度的运行状态。



等待线程的完成:

Waiting for a Single Task
You can wait for a single Task to complete by calling theWait() instance method. The calling method will not return until theTask instance has completed, been cancelled or thrown an exception.


using System;
using System.Threading;
using System.Threading.Tasks;
namespace Listing_16 {
class Listing_16 {
static void Main(string[] args) {
// create the cancellation token source
CancellationTokenSource tokenSource = new CancellationTokenSource();
// create the cancellation token
CancellationToken token = tokenSource.Token;
// create and start the first task, which we will let run fully
Task task = createTask(token);
task.Start();
// wait for the task
Console.WriteLine("Waiting for task to complete.");
task.Wait();
Console.WriteLine("Task Completed.");
// create and start another task
task = createTask(token);
task.Start();
Console.WriteLine("Waiting 2 secs for task to complete.");
bool completed = task.Wait(2000);
Console.WriteLine("Wait ended - task completed: {0}", completed);
// create and start another task
task = createTask(token);
task.Start();
Console.WriteLine("Waiting 2 secs for task to complete.");
completed = task.Wait(2000, token);
Console.WriteLine("Wait ended - task completed: {0} task cancelled {1}",
completed, task.IsCanceled);
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
static Task createTask(CancellationToken token) {
return new Task(() => {
for (int i = 0; i < 5; i++) {
// check for task cancellation
token.ThrowIfCancellationRequested();
// print out a message
Console.WriteLine("Task - Int value {0}", i);
// put the task to sleep for 1 second
token.WaitHandle.WaitOne(1000);
}
}, token);
}
}
}

Waiting for Several Tasks
You can wait for a number of tasks to complete by using the staticTask.WaitAll()method. This method will not return until all of the tasks passed as arguments have completed, been cancelled, or thrown an exception. 
TipWhen using this method, aTask is considered complete if it has finished its workload, been cancelled, or thrown an exception. If one or more of your tasks has thrown an exception, theWaitAll()method will throw an exception. See the “Handling Exceptions” section in this chapter for details.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Listing_17 {
class Listing_17 {
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 < 5; i++) {
// check for task cancellation
token.ThrowIfCancellationRequested();
// print out a message
Console.WriteLine("Task 1 - Int value {0}", i);
// put the task to sleep for 1 second
token.WaitHandle.WaitOne(1000);
}
Console.WriteLine("Task 1 complete");
}, token);
Task task2 = new Task(() => {
Console.WriteLine("Task 2 complete");
}, token);
// start the tasks
task1.Start();
task2.Start();
// wait for the tasks
Console.WriteLine("Waiting for tasks to complete.");
Task.WaitAll(task1, task2);
Console.WriteLine("Tasks Completed.");
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
}
}


Waiting for One of Many Tasks
The Task.WaitAny() method waits for one of a set of tasks to complete, and this method has a number of overloads, all of which take aTask array. 
TipWhen using this method, aTask is considered complete if it has finished its workload, been cancelled, or thrown an exception. If one or more of your tasks has thrown an exception, theWaitAny()method will throw an exception.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Listing_18 {
class Listing_18 {
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 < 5; i++) {
// check for task cancellation
token.ThrowIfCancellationRequested();
// print out a message
Console.WriteLine("Task 1 - Int value {0}", i);
// put the task to sleep for 1 second
token.WaitHandle.WaitOne(1000);
}
Console.WriteLine("Task 1 complete");
}, token);
Task task2 = new Task(() => {
Console.WriteLine("Task 2 complete");
}, token);
// start the tasks
task1.Start();
task2.Start();
// wait for the tasks
Console.WriteLine("Waiting for tasks to complete.");
int taskIndex = Task.WaitAny(task1, task2);
Console.WriteLine("Task Completed. Index: {0}", taskIndex);
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
}
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010155023/article/details/52354984
个人分类: Threading in C#
上一篇TPL——取消一个Task
下一篇TPL——Task的状态及堕性Task
想对作者说点什么? 我来说一句

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

关闭
关闭