在 .NET 4.0 下编写扩展代码以支持 async 异步编程

  微软在C# 5中引入了async、await这两个异步编程的关键字,要使用这两个关键字需要你的IDE支持C#5.0语法,也就意味着你需要使用VS 2012版本以上IDE,或者在Vs2010卸载其编译器在重新安装,这并不是本文的重点,有需求的朋友点击这篇博客,现在都快接近2019年,VS2019 预览版都出来了,现在使用Vs2010确实有点更不上时代了,建议大家升级一下vs,其他问题可以自行百度谷歌解决下。

  async和await带给我们的方便是巨大的,从代码的简洁或者处理UI阻塞问题,都是很便捷的,而XP系统最高只支持.Net 4.0,也就意味着,假如你编写的Winform程序使用了这两个关键字将不能在XP中,而目前很多系统仍然使用着XP,因此很有必要在.Net 4 环境下使用 async异步编程。

  当我们创建一个.Net Framework 4.0的项目时候,在使用async 和 await的时候,代码如下:

        static void Main(string[] args)
        {
            Console.WriteLine("异步方法调用前:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Test();
            Console.WriteLine("异步方法调用后:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Console.Read();
        }

        public async static void Test()
        {
            Console.WriteLine("异步方法等待前:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Console.WriteLine("开始等待:" + DateTime.Now.ToString());
            await Wait();
            Console.WriteLine("异步方法等待后:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Console.WriteLine("结束等待:" + DateTime.Now.ToString());
        }

        public static Task Wait()
        {
            Action action = () =>
            {
                Console.WriteLine("任务运行:" + Thread.CurrentThread.ManagedThreadId.ToString());
                Thread.Sleep(2000);
                Console.WriteLine("任务运行后:" + Thread.CurrentThread.ManagedThreadId.ToString());
            };
            Task task = null;
            //    task= TaskEx.Run(action);
            task = new Task(action);
            task.Start();
            return task;
        }

 

  这时候编译器会报一个错误 :CS1061 “Task”未包含“GetAwaiter”的定义,并且找不到可接受第一个“Task”类型参数的可访问扩展方法“GetAwaiter”(是否缺少 using 指令或程序集引用?) 

而在.Net 4.5下,是不会报这样错的。在异步的环境下,正确执行的情况下,Test()方法会单独开辟一个线程,所以整个测试,会在Main方法中Test()方法是异步的,具体执行顺序如下:

  回归到.Net 4.0中报的错:“Task”未包含“GetAwaiter”的定义,并且找不到可接受第一个“Task”类型参数的可访问扩展方法“GetAwaiter”。由此我们推断.Net 4.5 跟 .Net 4.0在处理异步的区别是否在于多了Task的扩展方法GetAwaiter()。

 

-------------------------------------------------------------------------------------华丽的过度线-------------------------------------------------------------------------------------------------------------------------

  由于涉及到 .net async 状态机,这东西目前不是目前我一下两下可以懂的,先将实现贴出来,以后学得深了在完善此随笔。

  新建一个类TaskEx,用如下的代码替换掉类里面的所有内容,会发现.Net 4.0下的报错会消失,也可以正常的使用异步async。有兴趣的同学可以在Github项目参考牛人写的代码

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace System.Threading.Tasks
{
    internal static class TaskEx
    {
        public static TaskAwaiter GetAwaiter(this Task task)
        {
            return new TaskAwaiter(task);
        }

        public static TaskAwaiter<T> GetAwaiter<T>(this Task<T> task)
        {
            return new TaskAwaiter<T>(task);
        }
        public static Task Run(Action action)
        {
            return Task.Factory.StartNew(action);
        }
    }
    internal struct TaskAwaiter : INotifyCompletion
    {
        readonly Task task;
        internal TaskAwaiter(Task task)
        {
            this.task = task;
        }

        internal static TaskScheduler TaskScheduler
        {
            get
            {
                if (SynchronizationContext.Current == null)
                    return TaskScheduler.Default;
                else
                    return TaskScheduler.FromCurrentSynchronizationContext();
            }
        }

        public bool IsCompleted
        {
            get { return task.IsCompleted; }
        }

        public void OnCompleted(Action continuation)
        {
            this.task.ContinueWith(
                delegate (Task task) {

                    //Console.WriteLine("状态机回调函数信息:"+Thread.CurrentThread.ManagedThreadId);
                    //Console.WriteLine("\t名称:"+continuation.Method.Name);
                    //Console.WriteLine("\t所属类型:" + continuation.Method.DeclaringType.ToString());
                    continuation();
                }, TaskAwaiter.TaskScheduler);
        }

        public void GetResult()
        {
            try
            {
                task.Wait();
            }
            catch (AggregateException ex)
            {
                throw ex.InnerExceptions[0];
            }
        }
    }

    internal struct TaskAwaiter<T> : INotifyCompletion
    {
        readonly Task<T> task;

        internal TaskAwaiter(Task<T> task)
        {
            this.task = task;
        }

        public bool IsCompleted
        {
            get { return task.IsCompleted; }
        }

        public void OnCompleted(Action continuation)
        {
            this.task.ContinueWith(
                delegate (Task<T> task) {
                    continuation();
                }, TaskAwaiter.TaskScheduler);
        }

        public T GetResult()
        {
            try
            {
                return task.Result;
            }
            catch (AggregateException ex)
            {
                throw ex.InnerExceptions[0];
            }
        }
    }
}

namespace System.Runtime.CompilerServices
{
    internal interface INotifyCompletion
    {
        void OnCompleted(Action continuation);
    }

    internal interface ICriticalNotifyCompletion : INotifyCompletion
    {
        [SecurityCritical]
        void UnsafeOnCompleted(Action continuation);
    }

    internal interface IAsyncStateMachine
    {
        void MoveNext();
        void SetStateMachine(IAsyncStateMachine stateMachine);
    }

    internal struct AsyncVoidMethodBuilder
    {
        public static AsyncVoidMethodBuilder Create()
        {
            return new AsyncVoidMethodBuilder();
        }

        public void SetException(Exception exception)
        {
            throw exception;
        }

        public void SetResult()
        {
        }

        public void SetStateMachine(IAsyncStateMachine stateMachine)
        {
            // Should not get called as we don't implement the optimization that this method is used for.
            throw new NotImplementedException();
        }

        public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
        {
            stateMachine.MoveNext();
        }

        public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
        {
            awaiter.OnCompleted(stateMachine.MoveNext);
        }

        public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
        {
            awaiter.OnCompleted(stateMachine.MoveNext);
        }
    }

    internal struct AsyncTaskMethodBuilder
    {
        TaskCompletionSource<object> tcs;

        public Task Task { get { return tcs.Task; } }

        public static AsyncTaskMethodBuilder Create()
        {
            AsyncTaskMethodBuilder b;
            b.tcs = new TaskCompletionSource<object>();
            return b;
        }

        public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
        {
            stateMachine.MoveNext();
        }

        public void SetStateMachine(IAsyncStateMachine stateMachine)
        {
            // Should not get called as we don't implement the optimization that this method is used for.
            throw new NotImplementedException();
        }

        public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
        {
            awaiter.OnCompleted(stateMachine.MoveNext);
        }

        public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
        {
            awaiter.OnCompleted(stateMachine.MoveNext);
        }

        public void SetResult()
        {
            tcs.SetResult(null);
        }

        public void SetException(Exception exception)
        {
            tcs.SetException(exception);
        }
    }

    internal struct AsyncTaskMethodBuilder<T>
    {
        TaskCompletionSource<T> tcs;

        public Task<T> Task { get { return tcs.Task; } }

        public static AsyncTaskMethodBuilder<T> Create()
        {
            AsyncTaskMethodBuilder<T> b;
            b.tcs = new TaskCompletionSource<T>();
            return b;
        }

        public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
        {
            stateMachine.MoveNext();
        }

        public void SetStateMachine(IAsyncStateMachine stateMachine)
        {
            // Should not get called as we don't implement the optimization that this method is used for.
            throw new NotImplementedException();
        }

        public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
        {
            awaiter.OnCompleted(stateMachine.MoveNext);
        }

        public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
        {
            AwaitOnCompleted(ref awaiter, ref stateMachine);
        }

        public void SetResult(T result)
        {
            tcs.SetResult(result);
        }

        public void SetException(Exception exception)
        {
            tcs.SetException(exception);
        }
    }
}

 

posted @ 2018-12-11 13:38  Fode  阅读(4924)  评论(0编辑  收藏  举报