1. 一句话理解异步

   我叫你去吃饭,叫完你不去,那我就会一直等你,直到你和我一起去吃饭。这叫同步!

   我叫你去吃饭,叫完不管你去不去,我都不会等你,我自己去吃饭。这叫异步!

2. 异步使用

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);


            Action<string> action = t =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
            };

            action.BeginInvoke("异步参数",null,null);

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

3. 异步回调

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);


            Action<string> action = t =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
            };
            AsyncCallback callback = t =>
            {
                Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
            };
            action.BeginInvoke("异步参数", callback, null);

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }


4. 异步回调带参数


        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Action<string> action = t =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
            };
            AsyncCallback callback = t =>
            {
                Console.WriteLine(t.AsyncState);
                Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
            };
            action.BeginInvoke("异步参数", callback, "无名小虾");

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }


5. 异步等待

第一种方式:IsCompleted

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Action<string> action = t =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
            };
            AsyncCallback callback = t =>
            {
                Console.WriteLine(t.AsyncState);
                Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
            };
            IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "无名小虾");

            while (!asyncResult.IsCompleted)
            {
                Console.WriteLine("等待中......");
                Thread.Sleep(200);
            }

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

第二种方式:WaitOne()

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Action<string> action = t =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
            };
            AsyncCallback callback = t =>
            {
                Console.WriteLine(t.AsyncState);
                Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
            };
            IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "无名小虾");

            asyncResult.AsyncWaitHandle.WaitOne();

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }

第三种方式:EndInvoke()

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Action<string> action = t =>
            {
                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
            };
            AsyncCallback callback = t =>
            {
                Console.WriteLine(t.AsyncState);
                Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
            };
            IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "无名小虾");

            action.EndInvoke(asyncResult);

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }


6. 异步返回值

主线程获取到返回值

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Func<string,string> func = t =>
            {
                string result = "这是我的返回值";

                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0},返回的参数是 {2}", Thread.CurrentThread.ManagedThreadId,t, result);
                return result;
            };

            IAsyncResult asyncResult = func.BeginInvoke("异步参数", null, null);

            string res = func.EndInvoke(asyncResult);

            Console.WriteLine("获取到返回值:{0}", res);

            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }


回调获取到返回值

        static void Main(string[] args)
        {
            Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Func<string,string> func = t =>
            {
                string result = "这是我的返回值";

                for (int k = 0; k < 1000000000; k++)
                { }
                Console.WriteLine("当前参数是{1},当前线程是{0},返回的参数是 {2}", Thread.CurrentThread.ManagedThreadId,t, result);
                return result;
            };

            AsyncCallback asyncCallback = t =>
            {
                string res = func.EndInvoke(t);
                Console.WriteLine("获取到返回值:{0}", res);
            };

            func.BeginInvoke("异步参数", asyncCallback, null);


            Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId);

            Console.Read();
        }