c# - Task Wait WaitAll WhenAll WhenAny

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace TaskWaitTesting
{
    class Program
    {
        private static Random R = new Random();

        static void Main(string[] args)
        {
            TestWait();
            //TestWaitAll(RetrieveTasks(10));
            //TestWhenAll(RetrieveTasks(10));
            //TestWaitAny(RetrieveTasks(10));
            //TestWhenAny(RetrieveTasks(10));

            // 主线程
            while (true)
            {
                Console.WriteLine("Main thread is working...");
                Thread.Sleep(2000);
            }
        }

        private static void TestWait()
        {
            Console.WriteLine(string.Format("================{0}================", MethodBase.GetCurrentMethod().Name));
            // Task with Action
            Task t1 = CreateATask(R.Next(1000, 10000));
            t1.Wait();

            // Task with Func
            Task t2 = Task.Factory.StartNew(s =>
            {
                Thread.Sleep(3000);
                Console.WriteLine(s);
                return s.ToString().Length;
            },
            "blabla");
            Console.WriteLine("Length:" + t2.Result);
            t2.Wait();

            Console.WriteLine("Completed.");
        }

        private static void TestWaitAll(List tasks)
        {
            // WaitAll(): 所有任务完成之后,才会向下执行
            Task.WaitAll(tasks.ToArray());
            Console.WriteLine("WaitAll() is end.");
        }

        private static void TestWaitAny(List tasks)
        {
            // WaitAny(): 任何一个任务完成之后,就会向下执行
            Task.WaitAny(tasks.ToArray());
            Console.WriteLine("WaitAny() is end.");
        }

        private static void TestWhenAll(List tasks)
        {
            // WhenAll():所有任务都完成之后,指定要做的事,不阻塞当前线程。
            // 此处的t1是一个额外的任务,与tasks无关
            var t1 = Task.WhenAll(tasks).ContinueWith(tt =>
            {
                Console.WriteLine("WhenAll() Completed.");
            });
        }

        private static void TestWhenAny(List tasks)
        {
            // WhenAny():任何一个任务都完成之后,指定要做的事,不阻塞当前线程。
            // 此处的t1是一个额外的任务,与tasks无关
            var t1 = Task.WhenAny(tasks).ContinueWith(tt =>
            {
                Console.WriteLine("WhenAny() Completed.");
            });
        }

        /// 
        /// 取得任务List
        /// 
        /// 
        /// 
        private static List RetrieveTasks(int cnt)
        {
            List tasks = new List();
            for (int i = 0; i < cnt; i++)
            {
                tasks.Add(CreateATask(R.Next(1000, 10000)));
            }
            return tasks;
        }

        /// 
        /// 创建一个任务
        /// 
        /// 
        /// 
        private static Task CreateATask(int time)
        {
            return Task.Factory.StartNew(input =>
            {
                Thread.Sleep(time);
                Console.WriteLine("Thread:" + Thread.CurrentThread.ManagedThreadId + " Task:" + Task.CurrentId + " worked " + input.ToString() + " ms, now it ends.");
            },
            time);
        }

        /// 
        /// 创建一个有返回内容的任务(未使用)
        /// 
        /// 
        /// 
        private static Task CreateATaskWithReturn(int time)
        {
            return Task.Factory.StartNew(input =>
            {
                Thread.Sleep(time);
                Console.WriteLine("Thread:" + Thread.CurrentThread.ManagedThreadId + " Task:" + Task.CurrentId + " worked " + input.ToString() + " ms, now it ends.");
                return Task.CurrentId.ToString();
            },
            time);
        }
    }
} 
  

你可能感兴趣的:(C#)