找传奇、传世资源到传世资源站!

Task使用(入门级)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

Task使用实例

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace TaskTest{ class Program { static void Main(string[] args) { #region Task的状态 //var task1 = new Task(() => //{ // Console.WriteLine("Begin"); // System.Threading.Thread.Sleep(2000); // Console.WriteLine("Finish"); //}); //Console.WriteLine("Before start:" task1.Status); //task1.Start(); //Console.WriteLine("After start:" task1.Status); //task1.Wait(); //Console.WriteLine("After Finish:" task1.Status); //Console.Read(); #endregion #region 多任务执行 //var task1 = new Task(() => //{ // Console.WriteLine("Task 1 Begin"); // System.Threading.Thread.Sleep(2000); // Console.WriteLine("Task 1 Finish"); //}); //var task2 = new Task(() => //{ // Console.WriteLine("Task 2 Begin"); // System.Threading.Thread.Sleep(3000); // Console.WriteLine("Task 2 Finish"); //}); //task1.Start(); //task2.Start(); //Task.WaitAll(task1, task2); //Console.WriteLine("All task finished!"); //Console.Read(); #endregion #region 等待任一个任务执行完毕 //var task1 = new Task(() => //{ // Console.WriteLine("Task 1 Begin"); // System.Threading.Thread.Sleep(2000); // Console.WriteLine("Task 1 Finish"); //}); //var task2 = new Task(() => //{ // Console.WriteLine("Task 2 Begin"); // System.Threading.Thread.Sleep(3000); // Console.WriteLine("Task 2 Finish"); //}); //task1.Start(); //task2.Start(); //Task.WaitAny(task1, task2); //Console.WriteLine("All task finished!"); //Console.Read(); #endregion #region 一个Task执行完成之后执行另一个Task //var task1 = new Task(() => //{ // Console.WriteLine("Task 1 Begin"); // System.Threading.Thread.Sleep(2000); // Console.WriteLine("Task 1 Finish"); //}); //var task2 = new Task(() => //{ // Console.WriteLine("Task 2 Begin"); // System.Threading.Thread.Sleep(3000); // Console.WriteLine("Task 2 Finish"); //}); //task1.Start(); //task2.Start(); //var result = task1.ContinueWith<string>(task => //{ // Console.WriteLine("task1 finished!"); // return "This is task result!"; //}); //Console.WriteLine(result.Result.ToString()); //Console.Read(); #endregion #region TaskContinuationOptions使用 //Task<Int32> t = new Task<Int32>(i => Sum((Int32)i), 10000); //t.Start(); //t.ContinueWith(task => Console.WriteLine("The sum is:{0}", task.Result), // TaskContinuationOptions.OnlyOnRanToCompletion); //t.ContinueWith(task => Console.WriteLine("Sum throw:" task.Exception), // TaskContinuationOptions.OnlyOnFaulted); //t.ContinueWith(task => Console.WriteLine("Sum was cancel:" task.IsCanceled), // TaskContinuationOptions.OnlyOnCanceled); //try //{ // t.Wait(); // 测试用 //} //catch (AggregateException) //{ // Console.WriteLine("出错"); //} //Console.Read(); #endregion Task<Int32[]> parent = new Task<Int32[]>(() => { var results = new Int32[3]; // new Task(() => results[0] = Sum(10000), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[1] = Sum(20000), TaskCreationOptions.AttachedToParent).Start(); new Task(() => results[2] = Sum(30000), TaskCreationOptions.AttachedToParent).Start(); return results; }); var cwt = parent.ContinueWith(parentTask => Array.ForEach(parentTask.Result, Console.WriteLine)); parent.Start(); cwt.Wait(); Console.Read(); } private static Int32 Sum(Int32 i) { Int32 sum = 0; for (; i > 0; i--) { checked { sum = i; } } return sum; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复