C# Task类 笔记

运行

namespace Example
{
    class MyApplication
    {
        public static void Main()
        {
            Task t = Task.Run(() =>
              {
                  for (int i = 0; i < 100; i++)
                      Console.WriteLine(i);
              });
            t.Wait();
        }
    }
}

返回一个值

namespace Example
{
    class MyApplication
    {
        public static void Main()
        {
            Task<int> t = Task.Run(() =>
              {
                  return 40;
              });
            Console.WriteLine(t.Result);   //输出40
        }
    }
}

ContinueWith

namespace Example
{
    class MyApplication
    {
        public static void Main()
        {
            Task<int> t = Task.Run(() =>
              {
                  return 40;
              });
            t.ContinueWith((i) =>
            {
                Console.WriteLine("Canceled");
            }, TaskContinuationOptions.OnlyOnCanceled);
            t.ContinueWith((i) =>
            {
                Console.WriteLine("Faulted");
            }, TaskContinuationOptions.OnlyOnFaulted);
            var completedtask=t.ContinueWith((i) =>
            {
                Console.WriteLine("Completed");
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
            completedtask.Wait();
            Console.WriteLine(t.Result); 
        }
    }
}

这里写图片描述

父线程将会在子线程结束后结束

namespace Example
{
    class MyApplication
    {
        public static void Main()
        {
            Task<Int32[]> parent = Task.Run(() =>
               {
                   var result = new Int32[3];
                   new Task(() => result[0] = 0, TaskCreationOptions.AttachedToParent).Start();
                   new Task(() => result[1] = 1, TaskCreationOptions.AttachedToParent).Start();
                   new Task(() => result[2] = 2, TaskCreationOptions.AttachedToParent).Start();
                   return result;
               });

            var finalTask = parent.ContinueWith(parentTask =>
            {
                foreach (int i in parentTask.Result)
                {
                    Console.WriteLine(i);
                }
            });

            finalTask.Wait();
        }
    }
}

使用TaskFactory

namespace Example
{
    class MyApplication
    {
        public static void Main()
        {
            Task<Int32[]> parent = Task.Run(() =>
               {
                   var result = new Int32[3];

                   TaskFactory tf = new TaskFactory(TaskCreationOptions.AttachedToParent, TaskContinuationOptions.ExecuteSynchronously);

                   tf.StartNew(() => result[0] = 0);
                   tf.StartNew(() => result[1] = 1);
                   tf.StartNew(() => result[2] = 2);
                   return result;
               });

            var finalTask = parent.ContinueWith(parentTask =>
            {
                foreach (int i in parentTask.Result)
                {
                    Console.WriteLine(i);
                }
            });

            finalTask.Wait();
        }
    }
}

Task.WaitAlL 等待提供的所有 Task 对象完成执行过程。

Task.WaitAny 等待提供的任何 Task 对象完成执行过程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cccccc1212

这是c币不是人民币,不要充值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值