为了篇幅关系,这里删除了部分复杂的实现,取而代之的是介绍方法作用的注释性文字,对于简单的方法或是重要的方法保留了代码。
namespace System.Runtime.CompilerServices { public struct AsyncTaskMethodBuilder<TResult> { internal static readonly Task<TResult> s_defaultResultTask = AsyncTaskCache.CreateCacheableTask<TResult>(default(TResult)); //这也是一个很重要的类,AsyncTaskMethodBuilder将一些操作进一步交给AsynchronousMethodBuilderCore来完成 private AsyncMethodBuilderCore m_coreState; private Task<TResult> m_task; [__DynamicallyInvokable] public Task<TResult> Task { [__DynamicallyInvokable] get { Task<TResult> task = this.m_task; if (task == null) { task = (this.m_task = new Task<TResult>()); } return task; } } private object ObjectIdForDebugger { get { return this.Task; } } [__DynamicallyInvokable] public static AsyncTaskMethodBuilder<TResult> Create() { return default(AsyncTaskMethodBuilder<TResult>); } //开始状态机的执行 [__DynamicallyInvokable, DebuggerStepThrough, SecuritySafeCritical] public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine { if (stateMachine == null) { throw new ArgumentNullException("stateMachine"); } //保存当前ExecutionContext,这是很重要的一步,后文会具体介绍 ExecutionContextSwitcher executionContextSwitcher = default(ExecutionContextSwitcher); RuntimeHelpers.PrepareConstrainedRegions(); try { ExecutionContext.EstablishCopyOnWriteScope(ref executionContextSwitcher); stateMachine.MoveNext(); } finally { executionContextSwitcher.Undo(); } } [__DynamicallyInvokable] public void SetStateMachine(IAsyncStateMachine stateMachine) { this.m_coreState.SetStateMachine(stateMachine); } [__DynamicallyInvokable] public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { try { AsyncMethodBuilderCore.MoveNextRunner runner = null; Action completionAction = this.m_coreState.GetCompletionAction(AsyncCausalityTracer.LoggingOn ? this.Task : null, ref runner); if (this.m_coreState.m_stateMachine == null) { Task<TResult> task = this.Task; this.m_coreState.PostBoxInitialization(stateMachine, runner, task); } awaiter.OnCompleted(completionAction); } catch (Exception arg_5C_0) { AsyncMethodBuilderCore.ThrowAsync(arg_5C_0, null); } } [__DynamicallyInvokable, SecuritySafeCritical] public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { try { AsyncMethodBuilderCore.MoveNextRunner runner = null; //这是整个方法乃至类中最重要的一部分 //获取当前状态执行完毕后下一步的操作 Action completionAction = this.m_coreState.GetCompletionAction(AsyncCausalityTracer.LoggingOn ? this.Task : null, ref runner); if (this.m_coreState.m_stateMachine == null) { Task<TResult> task = this.Task; this.m_coreState.PostBoxInitialization(stateMachine, runner, task); } //将下一步操作传递给awaiter对象,实际进入下一步还是通过awaiter来进行的。 awaiter.UnsafeOnCompleted(completionAction); } catch (Exception arg_5C_0) { AsyncMethodBuilderCore.ThrowAsync(arg_5C_0, null); } } [__DynamicallyInvokable] public void SetResult(TResult result) { //设置结果 //通过Task上的方法来完成 } internal void SetResult(Task<TResult> completedTask) { //设置结果,调用上面的方法来完成 } public void SetException(Exception exception) { //设置异常 //通过Task上的方法来实现 } internal void SetNotificationForWaitCompletion(bool enabled) { this.Task.SetNotificationForWaitCompletion(enabled); } private Task<TResult> GetTaskForResult(TResult result) { //获取Task包装的结果 } } }状态机的几种状态如下:
-1:表示还未开始执行
-2:执行结束,可能是正常完成,也可能遇到异常处理异常后结束
0~:下一个状态。如0表示初始的-1之后的下一个状态,1表示0后的下一状态,以此类推。