public void SetException(Exception exception) { if (exception == null) { throw new ArgumentNullException("exception"); } if (AsyncCausalityTracer.LoggingOn) { AsyncCausalityTracer.TraceOperationCompletion(CausalityTraceLevel.Required, this.Task.Id, AsyncCausalityStatus.Error); } if (this.m_synchronizationContext != null) { try { AsyncMethodBuilderCore.ThrowAsync(exception, this.m_synchronizationContext); } finally { this.NotifySynchronizationContextOfCompletion(); } } else { AsyncMethodBuilderCore.ThrowAsync(exception, (SynchronizationContext)null); } }
/// <summary>Faults the method builder with an exception.</summary> /// <param name="exception">The exception that is the cause of this fault.</param> /// <exception cref="System.ArgumentNullException">The <paramref name="exception"/> argument is null (Nothing in Visual Basic).</exception> /// <exception cref="System.InvalidOperationException">The builder is not initialized.</exception> public void SetException(Exception exception) { if (exception == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.exception); } if (AsyncCausalityTracer.LoggingOn) { AsyncCausalityTracer.TraceOperationCompletion(CausalityTraceLevel.Required, this.Task.Id, AsyncCausalityStatus.Error); } if (_synchronizationContext != null) { // If we captured a synchronization context, Post the throwing of the exception to it // and decrement its outstanding operation count. try { AsyncMethodBuilderCore.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 config // file or a CLR host. AsyncMethodBuilderCore.ThrowAsync(exception, targetContext: null); } // No need to call _builder.SetException, as no one pays attention to the task's completion. }
internal static void OnCompletedInternal(Task task, Action continuation, bool continueOnCapturedContext) { if (continuation == null) { throw new ArgumentNullException("continuation"); } SynchronizationContext sc = continueOnCapturedContext ? SynchronizationContext.Current : null; if (sc != null && sc.GetType() != typeof(SynchronizationContext)) { task.ContinueWith(delegate(Task param0) { try { sc.Post(delegate(object state) { ((Action)state)(); }, continuation); } catch (Exception exception) { AsyncMethodBuilderCore.ThrowAsync(exception, null); } }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); return; } TaskScheduler taskScheduler = continueOnCapturedContext ? TaskScheduler.Current : TaskScheduler.Default; if (task.IsCompleted) { Task.Factory.StartNew(delegate(object s) { ((Action)s)(); }, continuation, CancellationToken.None, TaskCreationOptions.None, taskScheduler); return; } if (taskScheduler != TaskScheduler.Default) { task.ContinueWith(delegate(Task _) { TaskAwaiter.RunNoException(continuation); }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, taskScheduler); return; } task.ContinueWith(delegate(Task param0) { if (TaskAwaiter.IsValidLocationForInlining) { TaskAwaiter.RunNoException(continuation); return; } Task.Factory.StartNew(delegate(object s) { TaskAwaiter.RunNoException((Action)s); }, continuation, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); }
/// <summary> /// Invokes the delegate in a try/catch that will propagate the exception asynchronously on the ThreadPool. /// </summary> /// <param name="continuation" /> private static void RunNoException(Action continuation) { try { continuation(); } catch (Exception ex) { AsyncMethodBuilderCore.ThrowAsync(ex, null); } }
/// <summary> /// Notifies the current synchronization context that the operation completed. /// </summary> private void NotifySynchronizationContextOfCompletion() { try { m_synchronizationContext.OperationCompleted(); } catch (Exception ex) { AsyncMethodBuilderCore.ThrowAsync(ex, null); } }
/// <summary> /// Schedules the continuation onto the <see cref="Task" /> associated with this <see cref="TaskAwaiter" /> . /// </summary> /// <param name="task"> The awaited task. </param> /// <param name="continuation"> The action to invoke when the await operation completes. </param> /// <param name="continueOnCapturedContext"> Whether to capture and marshal back to the current context. </param> /// <exception cref="ArgumentNullException">The /// <paramref name="continuation" /> /// argument is null (Nothing in Visual Basic).</exception> /// <exception cref="NullReferenceException">The awaiter was not properly initialized.</exception> /// <remarks> /// This method is intended for compiler user rather than use directly in code. /// </remarks> internal static void OnCompletedInternal(Task task, Action continuation, bool continueOnCapturedContext) { if (continuation == null) { throw new ArgumentNullException("continuation"); } var syncContext = continueOnCapturedContext ? SynchronizationContext.Current : null; if (syncContext != null && syncContext.GetType() != typeof(SynchronizationContext)) { task.ContinueWith(result => { try { syncContext.Post(state => ((Action)state)(), continuation); } catch (Exception ex) { AsyncMethodBuilderCore.ThrowAsync(ex, null); } }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); } else { var scheduler = continueOnCapturedContext ? TaskScheduler.Current : TaskScheduler.Default; if (task.IsCompleted) { Task.Factory.StartNew(state => ((Action)state)(), continuation, CancellationToken.None, TaskCreationOptions.None, scheduler); } else if (scheduler != TaskScheduler.Default) { task.ContinueWith(_ => RunNoException(continuation), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, scheduler); } else { task.ContinueWith(result => { if (IsValidLocationForInlining) { RunNoException(continuation); } else { Task.Factory.StartNew(state => RunNoException((Action)state), continuation, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); } }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); } } }
/// <summary> /// Schedules the state machine to proceed to the next action when the specified awaiter completes. /// </summary> /// <param name="awaiter">The awaiter.</param> /// <param name="stateMachine">The state machine.</param> /// <typeparam name="TAwaiter">The type of the awaiter.</typeparam> /// <typeparam name="TStateMachine">The type of the state machine.</typeparam> public void AwaitOnCompleted <TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { try { Action completionAction = m_coreState.GetCompletionAction(this, stateMachine); awaiter.OnCompleted(completionAction); } catch (Exception ex) { AsyncMethodBuilderCore.ThrowAsync(ex); } }
/// <summary> /// Schedules the state machine to proceed to the next action when the specified awaiter completes. /// </summary> /// <param name="awaiter">The awaiter.</param> /// <param name="stateMachine">The state machine.</param> /// <typeparam name="TAwaiter">The type of the awaiter.</typeparam> /// <typeparam name="TStateMachine">The type of the state machine.</typeparam> public void TrueAwaitOnCompleted(INotifyCompletion awaiter, IAsyncStateMachine stateMachine) { try { Action completionAction = m_coreState.GetCompletionAction(this, stateMachine); awaiter.OnCompleted(completionAction); } catch (Exception ex) { AsyncMethodBuilderCore.ThrowAsync(ex); } }
private void NotifySynchronizationContextOfCompletion() { try { this.m_synchronizationContext.OperationCompleted(); } catch (Exception ex) { // ISSUE: variable of the null type __Null local = null; AsyncMethodBuilderCore.ThrowAsync(ex, (SynchronizationContext)local); } }
public void AwaitUnsafeOnCompleted <TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { try { var completionAction = m_coreState.GetCompletionAction(ref this, ref stateMachine); awaiter.UnsafeOnCompleted(completionAction); } catch (Exception ex) { AsyncMethodBuilderCore.ThrowAsync(ex, 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 ThreadPool. AsyncMethodBuilderCore.ThrowAsync(exc, targetContext: null); } }
public void SetException(Exception exception) { Task taskIfDebuggingEnabled = this.GetTaskIfDebuggingEnabled(); if (taskIfDebuggingEnabled != null) { if (DebuggerSupport.LoggingOn) { DebuggerSupport.TraceOperationCompletion(CausalityTraceLevel.Required, taskIfDebuggingEnabled, AsyncStatus.Error); } } AsyncMethodBuilderCore.ThrowAsync(exception, m_synchronizationContext); NotifySynchronizationContextOfCompletion(); }
private void NotifySynchronizationContextOfCompletion() { if (m_synchronizationContext != null) { try { m_synchronizationContext.OperationCompleted(); } catch (Exception exc) { // If the interaction with the SynchronizationContext goes awry, // fall back to propagating on the ThreadPool. AsyncMethodBuilderCore.ThrowAsync(exc, targetContext: null); } } }
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 exception) { AsyncMethodBuilderCore.ThrowAsync(exception, null); } }
public void AwaitUnsafeOnCompleted <TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { try { AsyncMethodBuilderCore.MoveNextRunner runnerToInitialize = (AsyncMethodBuilderCore.MoveNextRunner)null; Action completionAction = this.m_coreState.GetCompletionAction(AsyncCausalityTracer.LoggingOn ? (System.Threading.Tasks.Task) this.Task : (System.Threading.Tasks.Task)null, ref runnerToInitialize); if (this.m_coreState.m_stateMachine == null) { System.Threading.Tasks.Task <TResult> task = this.Task; this.m_coreState.PostBoxInitialization((IAsyncStateMachine)stateMachine, runnerToInitialize, (System.Threading.Tasks.Task)task); } awaiter.UnsafeOnCompleted(completionAction); } catch (Exception ex) { // ISSUE: variable of the null type __Null local = null; AsyncMethodBuilderCore.ThrowAsync(ex, (SynchronizationContext)local); } }
public void SetException(Exception exception) { if (exception == null) { throw new ArgumentNullException("exception"); } if (this.m_synchronizationContext != null) { try { AsyncMethodBuilderCore.ThrowAsync(exception, this.m_synchronizationContext); return; } finally { this.NotifySynchronizationContextOfCompletion(); } } AsyncMethodBuilderCore.ThrowAsync(exception, null); }
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) { if (AsyncCausalityTracer.LoggingOn) { AsyncCausalityTracer.TraceOperationCreation(CausalityTraceLevel.Required, this.Task.Id, "Async: " + stateMachine.GetType().Name, 0UL); } this.m_coreState.PostBoxInitialization(stateMachine, runner, null); } awaiter.OnCompleted(completionAction); } catch (Exception exception) { AsyncMethodBuilderCore.ThrowAsync(exception, null); } }
public void AwaitUnsafeOnCompleted <TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { try { AsyncMethodBuilderCore.MoveNextRunner runnerToInitialize = (AsyncMethodBuilderCore.MoveNextRunner)null; Action completionAction = this.m_coreState.GetCompletionAction(AsyncCausalityTracer.LoggingOn ? this.Task : (Task)null, ref runnerToInitialize); if (this.m_coreState.m_stateMachine == null) { if (AsyncCausalityTracer.LoggingOn) { AsyncCausalityTracer.TraceOperationCreation(CausalityTraceLevel.Required, this.Task.Id, "Async: " + stateMachine.GetType().Name, 0UL); } this.m_coreState.PostBoxInitialization((IAsyncStateMachine)stateMachine, runnerToInitialize, (Task)null); } awaiter.UnsafeOnCompleted(completionAction); } catch (Exception ex) { // ISSUE: variable of the null type __Null local = null; AsyncMethodBuilderCore.ThrowAsync(ex, (SynchronizationContext)local); } }