首页 > 其他 > 详细

Task学习

时间:2015-03-09 12:33:02      阅读:339      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!