static private ThrowAsync ( Exception exception, object targetContext ) : void | ||
exception | Exception | |
targetContext | object | |
return | void |
/// <summary>指定的 awaiter 完成时,安排状态机,以继续下一操作。</summary> /// <typeparam name="TAwaiter"></typeparam> /// <typeparam name="TStateMachine"></typeparam> /// <param name="awaiter"></param> /// <param name="stateMachine"></param> public void AwaitOnCompleted <TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { try { Action completionAction = m_coreState.GetCompletionAction(ref this, ref stateMachine); awaiter.OnCompleted(completionAction); } catch (Exception exception) { AsyncServices.ThrowAsync(exception, null); } }
/// <summary>Notifies the current synchronization context that the operation completed.</summary> private void NotifySynchronizationContextOfCompletion() { Debug.Assert(_synchronizationContext != null, "Must only be used with a non-null context."); try { _synchronizationContext.OperationCompleted(); } catch (Exception exc) { // If the interaction with the SynchronizationContext goes awry, fall back to propagating on the // thread pool. AsyncServices.ThrowAsync(exc, targetContext: null); } }
/// <summary> /// Schedules the specified state machine to be pushed forward when the specified awaiter completes. /// </summary> /// <typeparam name="TAwaiter">Specifies the type of the awaiter.</typeparam> /// <typeparam name="TStateMachine">Specifies the type of the state machine.</typeparam> /// <param name="awaiter">The awaiter.</param> /// <param name="stateMachine">The state machine.</param> public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { try { var continuation = _coreState.GetCompletionAction(ref this, ref stateMachine); Debug.Assert(continuation != null, "GetCompletionAction should always return a valid action."); awaiter.UnsafeOnCompleted(continuation); } catch (Exception e) { AsyncServices.ThrowAsync(e, targetContext: null); } }
/// <summary> /// Schedules the specified state machine to be pushed forward when the specified awaiter completes. /// </summary> /// <typeparam name="TAwaiter">Specifies the type of the awaiter.</typeparam> /// <typeparam name="TStateMachine">Specifies the type of the state machine.</typeparam> /// <param name="awaiter">The awaiter.</param> /// <param name="stateMachine">The state machine.</param> #if !SILVERLIGHT // [SecuritySafeCritical] #endif public void AwaitUnsafeOnCompleted <TAwaiter, TStateMachine>( ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { try { var continuation = m_coreState.GetCompletionAction(ref this, ref stateMachine); awaiter.UnsafeOnCompleted(continuation); } catch (Exception e) { AsyncServices.ThrowAsync(e, targetContext: null); } }
/// <summary>将一个异常绑定到该方法生成器。</summary> /// <param name="exception"></param> public void SetException(Exception exception) { if (exception == null) { throw new ArgumentNullException("exception"); } if (m_synchronizationContext != null) { try { AsyncServices.ThrowAsync(exception, m_synchronizationContext); return; } finally { NotifySynchronizationContextOfCompletion(); } } AsyncServices.ThrowAsync(exception, null); }
/// <summary> /// Schedules the specified state machine to be pushed forward when the specified awaiter completes. /// </summary> /// <typeparam name="TAwaiter">Specifies the type of the awaiter.</typeparam> /// <typeparam name="TStateMachine">Specifies the type of the state machine.</typeparam> /// <param name="awaiter">The awaiter.</param> /// <param name="stateMachine">The state machine.</param> public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { try { var continuation = _coreState.GetCompletionAction(ref this, ref stateMachine); Debug.Assert(continuation != null, "GetCompletionAction should always return a valid action."); awaiter.OnCompleted(continuation); } catch (Exception exc) { // Prevent exceptions from leaking to the call site, which could // then allow multiple flows of execution through the same async method // if the awaiter had already scheduled the continuation by the time // the exception was thrown. We propagate the exception on the // ThreadPool because we can trust it to not throw, unlike // if we were to go to a user-supplied SynchronizationContext, // whose Post method could easily throw. AsyncServices.ThrowAsync(exc, targetContext: null); } }