from http://ggicci.blog.163.com/blog/static/210364096201272053644956/
Title :
- Pro .NET 4 Parallel Programming in C# (Adam Freeman) - Waiting for a Single Task
- C#多核编程 线程等待
Quote :
Task.Wait() 的多个重载:
- Wait() 等待,直到 Task 完成,或者被取消,或者抛出异常
- Wait(CancallationToken) 等待,直到 CancellationToken 被取消,或者同上
- Wait(Int32) 等待,直到 Int32 代表的时间过了,或者同 Wait()
- Wait(TimeSpan) 等待,直到 TimeSpan 所代表的时间过了,或者同 Wait()
- Wait(Int32, CancellationToken) 等待,直到…(就是第二个和第三个的组合啦)
Code 1 : [ Wait() ]
1: public static void Main(string[] args)2: {
3: CancellationTokenSource tokenSource = new CancellationTokenSource();4: CancellationToken token = tokenSource.Token;
5:
6: Task task = new Task(() =>7: {
8: token.ThrowIfCancellationRequested();
9: Thread.Sleep(1000);
10: Console.WriteLine("task running...");11: }, token);
12: task.Start();
13:
14: //Console.WriteLine("Wait for task to complete.");15: //task.Wait();16:
17: Console.WriteLine("Press enter to finish.");18: Console.ReadLine();
19: }
Result 1.1 : 下面的 “task running…”在 1000 ms 后打印
1: Press enter to finish.
2: task running...
如果去掉代码中的注释部分,那么输出结果是:
Result 1.2 : 主线程会等待 task 完成
1: Wait for task to complete.
2: task running...
3: Press enter to finish.
Code 2 : [ Wait(Int32) ]
1: public static void Main(string[] args)2: {
3: CancellationTokenSource tokenSource = new CancellationTokenSource();4: CancellationToken token = tokenSource.Token;
5:
6: Task task = new Task(() =>7: {
8: token.ThrowIfCancellationRequested();
9: Thread.Sleep(1000);
10: Console.WriteLine("task running...");11: }, token);
12: task.Start();
13:
14: //等待 task 500ms, 如果 500ms 内 task 完成, 那么会在 task 完成后马上返回 true15: //如果 500 ms 内 task 没有完成, 那么返回 false16: bool completed = task.Wait(500);17: Console.WriteLine("Task completed? {0}", completed);18:
19: Console.WriteLine("Press enter to finish.");20: Console.ReadLine();
21: }
Result 2 : 500 ms 过去后打印 completed 的值,为 false,然后 task 在主线程结束后结束
1: Task completed? False
2: Press enter to finish.
3: task running...
Code 3 : [ Wait(Int32, CancellationToken) ]
1: public static void Main(string[] args)2: {
3: CancellationTokenSource tokenSource = new CancellationTokenSource();4: CancellationToken token = tokenSource.Token;
5:
6: Task task = new Task(() =>7: {
8: bool canceled = token.WaitHandle.WaitOne(1000);9: if (canceled)10: {
11: throw new OperationCanceledException(token);12: }
13: Console.WriteLine("task running...");14: }, token);
15:
16: //timeTask 的任务是在 500 ms 过去的时候调用 tokenSource 的 Cancel 方法以取消 task17: Task timeTask = new Task(() =>18: {
19: Thread.Sleep(500);
20: tokenSource.Cancel();
21: });
22: task.Start();
23: timeTask.Start();
24:
25: try26: {
27: //task 如果在执行过程中被取消, 那么就会抛出 OperationCanceledException28: bool completed = task.Wait(2000, token);29: Console.WriteLine("Task completed? {0}", completed);30: }
31: catch (OperationCanceledException ex)32: {
33: Console.WriteLine("Exception: {0}", ex.GetType());34:
35: }
36:
37: Console.WriteLine("Press enter to finish.");38: Console.ReadLine();
39: }
Result 3 :
1: Exception: System.OperationCanceledException
2: Press enter to finish.
End :
Author : Ggicci
此篇属于学习笔记,如有错误,欢迎指正!