码迷,mamicode.com
首页 > 其他好文 > 详细

Task学习

时间:2015-03-09 12:35:33      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

1. 普通任务

技术分享普通任务

技术分享

2. WaitTask

技术分享
 1         static void Main(string[] args)
 2         {
 3             Console.WriteLine("Main Thread Processing");
 4             waitTaskFinished();
 5         }
 6 
 7         private static void waitTaskFinished()
 8         {
 9             var task = new Task<int>(CalculateSum);
10             task.Start();
11             Console.WriteLine("Main Thread Processing Other");
12 
13             // The main thread would wait the task finished and then continue the following works.
14             task.Wait();
15             Console.WriteLine(task.Result);
16             Console.WriteLine("Main Thread Processing Finished");
17             Console.ReadLine();
18 
19         }
20 
21         private static int CalculateSum()
22         {
23             Console.WriteLine("Task Proceessing");
24             int sum = 0;
25             for (int t = 0; t < 100; t++)
26             {
27                 sum += t;
28             }
29             Console.WriteLine("Task Proceessing Finished");
30             return sum;
31         }
等待Task结束

技术分享

 

 

后续可以参考: http://www.cnblogs.com/pengstone/archive/2012/12/23/2830238.html

 

Task学习

标签:

原文地址:http://www.cnblogs.com/Alf7/p/4322950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!