async await

用於處理IO消耗的多線程html

        static void Main(string[] args)
        {
            var info = Hello().Result;

            Console.WriteLine(info);

            Console.Read();
        }

        static async Task<string> Hello()
        {
            //主線程執行,底層還會調用一個 AwaitUnsafeOnCompleted 委託給線程池
            Console.WriteLine("hello world");

            //在工做線程中執行
            var x = await Task.Run(() =>
              {
                  Console.WriteLine("i'm middle");

                  return "i'm ok";
              });

            Console.WriteLine("我是結尾哦:{0}", x);

            return x;
        }

  

  • AsyncTaskMethodBuilder: 扮演了一個TaskcomplationSource一個角色,就是作Task的包裝器。
  • state:扮演者狀態機狀態的角色,記錄當前狀態
  • AwaitUnsafeOnCompleted :這個函數是丟給線程池去執行的,當某一時刻執行結束,會調用Movenext
    class Program
    {
        static void Main(string[] args)
        {
            var info = Hello();

            Console.WriteLine("我去: " + info.Result);

            Console.Read();
        }

        static Task<string> Hello()
        {
            MyStateMachine machine = new MyStateMachine();

            //相似TaskCompletionSource,包裝器
            machine.t_builder = AsyncTaskMethodBuilder<string>.Create();            

            machine.state = -1;

            var t_builder = machine.t_builder;

            t_builder.Start(ref machine);

            return machine.t_builder.Task;
        }
    }

    public class MyStateMachine : IAsyncStateMachine
    {
        public AsyncTaskMethodBuilder<string> t_builder;

        public int state;

        private MyStateMachine machine = null;

        private TaskAwaiter<string> myawaiter;

        string result = string.Empty;

        public MyStateMachine()
        {
        }

        public void MoveNext()
        {
            try
            {
                switch (state)
                {
                    case -1:
                        Console.WriteLine("hello world");

                        var waiter = Task.Run(() =>
                        {
                            Console.WriteLine("i'm middle");

                            return "i'm ok";
                        }).GetAwaiter();

                        state = 0;  //設置下一個狀態
                        myawaiter = waiter;

                        machine = this;

                        //丟給線程池執行了。。。
                        t_builder.AwaitUnsafeOnCompleted(ref waiter, ref machine);
                        break;

                    case 0:

                        var j = myawaiter.GetResult();

                        Console.WriteLine("我是結尾哦:{0}", j);

                        t_builder.SetResult(j);
                        break;
                }
            }
            catch (Exception ex)
            {
                t_builder.SetException(ex);  //設置t_builder的異常
            }
        }

        public void SetStateMachine(IAsyncStateMachine stateMachine)
        {
        }
    }

 

異步執行IO操做多線程

 線程池將Task交給驅動程序的隊列,當即返回。異步

 等文件系統操做完成後,通知CLR,CLR使用IO線程通知應用程序(調用回調函數)async

同步阻塞執行IO操做函數

 

 

 

 

?GetAwaiter?ui

 

 

 

 

http://www.javashuo.com/article/p-uqrfdmgz-ek.htmlthis

相關文章
相關標籤/搜索