Example #1
0
        /// <summary>
        /// Runs the in specified action in asynchronous context and waits for all asynchronous actions to complete.
        /// </summary>
        /// <param name="action">The action.</param>
        public static void RunInAsyncContext(Action action)
        {
            ReadOnlyCollection <Action <IAsyncContinuation> > actions;

            using (var context = AsyncExecutionContext.Begin())
            {
                action();
                actions = context.GetQueuedActions();
            }

            using (ManualResetEvent finishedEvent = new ManualResetEvent(false))
            {
                Exception lastException = null;

                var continuation = CreateContinuation(
                    () => finishedEvent.Set(),
                    ex =>
                {
                    lastException = ex;
                    finishedEvent.Set();
                });

                AsyncHelpers.RunActionSequence(continuation, actions);
                finishedEvent.WaitOne();

                if (lastException != null)
                {
                    throw new TaupoInvalidOperationException("Error during asynchronous execution, see inner message for details.", lastException);
                }
            }
        }
        /// <summary>
        /// Ends this execution context.
        /// </summary>
        public void End()
        {
            lock (lockObject)
            {
                if (current != this)
                {
                    throw new TaupoInvalidOperationException("Cannot end context other than current.");
                }

                current = null;
            }
        }
        /// <summary>
        /// Begins new asynchronous execution context, returns it and assigns it to Current
        /// </summary>
        /// <returns>Instance of <see cref="AsyncExecutionContext"/> which can be disposed later when the execution ends.</returns>
        /// <remarks>This method is to be used internally by test runners to set up execution context for variations</remarks>
        public static AsyncExecutionContext Begin()
        {
            lock (lockObject)
            {
                if (current != null)
                {
                    throw new TaupoInvalidOperationException("Cannot nest asynchronous execution contexts.");
                }

                current = new AsyncExecutionContext();
                return(current);
            }
        }
        /// <summary>
        /// Begins new asynchronous execution context, returns it and assigns it to Current
        /// </summary>
        /// <returns>Instance of <see cref="AsyncExecutionContext"/> which can be disposed later when the execution ends.</returns>
        /// <remarks>This method is to be used internally by test runners to set up execution context for variations</remarks>
        public static AsyncExecutionContext Begin()
        {
            lock (lockObject)
            {
                if (current != null)
                {
                    throw new TaupoInvalidOperationException("Cannot nest asynchronous execution contexts.");
                }

                current = new AsyncExecutionContext();
                return current;
            }
        }
        /// <summary>
        /// Ends this execution context.
        /// </summary>
        public void End()
        {
            lock (lockObject)
            {
                if (current != this)
                {
                    throw new TaupoInvalidOperationException("Cannot end context other than current.");
                }

                current = null;
            }
        }