internal static void ThrowAsync(Exception exception, SynchronizationContext targetContext) { if (targetContext != null) { try { targetContext.Post(delegate(object state) { throw AsyncServices.PrepareExceptionForRethrow((Exception)state); }, exception); return; } catch (Exception ex) { exception = new AggregateException(new Exception[] { exception, ex }); } } ThreadPool.QueueUserWorkItem(delegate(object state) { throw AsyncServices.PrepareExceptionForRethrow((Exception)state); }, exception); }
/// <summary>Faults the method builder with an exception.</summary> /// <param name="exception">The exception that is the cause of this fault.</param> /// <exception cref="ArgumentNullException"> /// If <paramref name="exception"/> is <see langword="null"/>. /// </exception> /// <exception cref="InvalidOperationException">The builder is not initialized.</exception> public void SetException(Exception exception) { if (exception == null) throw new ArgumentNullException("exception"); if (_synchronizationContext != null) { // If we captured a synchronization context, post the throwing of the exception to it and decrement its // outstanding operation count. try { AsyncServices.ThrowAsync(exception, targetContext: _synchronizationContext); } finally { NotifySynchronizationContextOfCompletion(); } } else { // Otherwise, queue the exception to be thrown on the ThreadPool. This will result in a crash unless // legacy exception behavior is enabled by a configuration file or a CLR host. AsyncServices.ThrowAsync(exception, targetContext: null); } }
private void NotifySynchronizationContextOfCompletion() { try { m_synchronizationContext.OperationCompleted(); } catch (Exception exception) { AsyncServices.ThrowAsync(exception, null); } }
/// <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 <AsyncVoidMethodBuilder, TStateMachine>(ref this, ref stateMachine); awaiter.OnCompleted(completionAction); } catch (Exception exception) { AsyncServices.ThrowAsync(exception, null); } }
/// <summary>指定的 awaiter 完成时,安排状态机,以继续下一操作。此方法可从部分受信任的代码调用。</summary> /// <typeparam name="TAwaiter"></typeparam> /// <typeparam name="TStateMachine"></typeparam> /// <param name="awaiter"></param> /// <param name="stateMachine"></param> public void AwaitUnsafeOnCompleted <TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { try { Action completionAction = m_coreState.GetCompletionAction(ref this, ref stateMachine); awaiter.UnsafeOnCompleted(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> public void AwaitOnCompleted <TAwaiter, TStateMachine>( ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { try { var continuation = m_coreState.GetCompletionAction(ref this, ref stateMachine); awaiter.OnCompleted(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); } }