简单了解async-await组合使用多线程异步,一看就会

 static void Main(string[] args)
        {
            //调用 Method1  Method2  Method3 三个方法会同时执行, 甚至加上Method4一起调用也会四个方法同时执行 
            Method1();
            Method2();
            Method3();

            //当以此顺序同时运行这五个方法时,前面Method 1-4 会先运行,等Method4运行完才会执行Method5方法
            //Method1(); async await 异步方法
            //Method2(); async await 异步方法
            //Method3(); async await 异步方法
            //Method4(); 常用输出方法
            //Method5(); 常用输出方法

            Console.ReadKey();
        }
        
        public static async Task Method1()
        {
            await Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(" Method 1S" + i);
                }
            });
        }
        public static async Task Method2()
        {
            await Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(" Method 2W" + i);
                }
            });  
        }
        public static async Task Method3()
        {
            await Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(" Method 3Y" + i);
                }
            });
        }
        public static void Method4()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(" Method 4P" + i);
            }
        }
        public static void Method5()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(" Method 5T" + i);
            }
        }
static void Main(string[] args)
        {
            callMethod();
            Console.ReadKey();
        }
       

        public static async void callMethod()
        {
            Task task = Method1();
            //Thread.Sleep(1000); 如没延时,当程序在执行Method1的时候Method2就已经执行完了
            Method2();
            int count = await task;
            Method3(count);
        }

        public static async Task Method1()
        {
            int count = 0;
            await Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(" Method 1");
                    count += 1;
                }
            });
            return count;
        }

        public static void Method2()
        {
            for (int i = 0; i < 25; i++)
            {
                Console.WriteLine(" Method 2");
            }
        }

        public static void Method3(int count)
        {
            Console.WriteLine("Total count is " + count);
        }
static void Main(string[] args)
        {
            Task task = new Task(CallMethod);
            task.Start();//启动 Task
            task.Wait();//等待 Task 完成执行过程
            Console.ReadLine();
        }


        static async void CallMethod()
        {
            Console.WriteLine(" 进入CallMethod方法");
            string filePath = "H:\\sampleFile.txt";

            Task task = ReadFile(filePath);//

            Console.WriteLine("进入CallMethod方法 2");

            string text = await task;

            //int length = await task;
            Console.WriteLine(" Total length: " + text);
             Console.WriteLine("结束");

        }

        static async Task ReadFile(string file)
        {
            string text = string.Empty;
            //int length = 0;
            Console.WriteLine(" 进入ReadFile方法");
            using (StreamReader reader = new StreamReader(file))//文件流
            {
                string s = await reader.ReadToEndAsync();//读取tet文件文本内容

                //length = s.Length;//读取tet文件文本内容长度
                text = s;
            }
            //return length;
            return text;
        }
static void Main(string[] args)
        {
            Task task = new Task(CallMethod);
            task.Start();//启动 Task
            task.Wait();//等待 Task 完成执行过程
            Console.ReadLine();
        }


        static async void CallMethod()
        {
            Console.WriteLine(" 进入CallMethod方法");

            Show1();

            Console.WriteLine(" 进入CallMethod方法 2");
           
            //int length = await task;
            
            Console.WriteLine("结束");

        }
        public static async void Show1() 
        {
            Console.WriteLine(" 进入Show1方法");
            string filePath = "H:\\sampleFile.txt";

            Task task = ReadFile(filePath);//

            string text = await task;
            await Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(" Total length: " + text + "Y" + i);
                }

            });
        }

        static async Task ReadFile(string file)
        {
            string text = string.Empty;
            //int length = 0;
            Console.WriteLine(" 进入ReadFile方法");
            using (StreamReader reader = new StreamReader(file))//文件流
            {
                string s = await reader.ReadToEndAsync();//读取tet文件文本内容

                //length = s.Length;//读取tet文件文本内容长度
                text = s;
            }
            //return length;
            return text;
        }

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