Ejemplo n.º 1
0
        internal static void Start <TStateMachine>(ref TStateMachine stateMachine)
            where TStateMachine : IAsyncStateMachine
        {
            // Async state machines are required not to throw, so no need for try/finally here.
            Thread currentThread         = Thread.CurrentThread;
            ExecutionContextSwitcher ecs = default(ExecutionContextSwitcher);

            ExecutionContext.EstablishCopyOnWriteScope(currentThread, ref ecs);
            stateMachine.MoveNext();
            ecs.Undo(currentThread);
        }
Ejemplo n.º 2
0
        public void Start <TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
        {
            if (stateMachine == null)
            {
                throw new ArgumentNullException("stateMachine");
            }
            ExecutionContextSwitcher executionContextSwitcher = default(ExecutionContextSwitcher);

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                ExecutionContext.EstablishCopyOnWriteScope(ref executionContextSwitcher);
                stateMachine.MoveNext();
            }
            finally
            {
                executionContextSwitcher.Undo();
            }
        }
Ejemplo n.º 3
0
        public void Start <TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
        {
            if (stateMachine == null) // TStateMachines are generally non-nullable value types, so this check will be elided
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.stateMachine);
            }

            // Run the MoveNext method within a copy-on-write ExecutionContext scope.
            // This allows us to undo any ExecutionContext changes made in MoveNext,
            // so that they won't "leak" out of the first await.

            Thread currentThread         = Thread.CurrentThread;
            ExecutionContextSwitcher ecs = default(ExecutionContextSwitcher);

            try
            {
                ExecutionContext.EstablishCopyOnWriteScope(currentThread, ref ecs);
                stateMachine.MoveNext();
            }
            finally
            {
                ecs.Undo(currentThread);
            }
        }