public override IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary<string, object> parameters, AsyncCallback callback, object state) {
            if (controllerContext == null) {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null) {
                throw new ArgumentNullException("parameters");
            }

            AsyncManager asyncHelper = GetAsyncManager(controllerContext.Controller);
            SingleEntryGate setupCompletedGate = new SingleEntryGate();

            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult() {
                    AsyncState = innerState
                };

                // Get parameters for async setup method, then execute
                ParameterInfo[] setupParametersInfos = SetupMethod.GetParameters();
                var rawSetupParameterValues = from parameterInfo in setupParametersInfos
                                              select ExtractParameterFromDictionary(parameterInfo, parameters, SetupMethod);
                object[] setupParametersArray = rawSetupParameterValues.ToArray();

                // to simplify the logic, force an asynchronous callback
                asyncHelper.OutstandingOperations.Completed += delegate {
                    if (setupCompletedGate.TryEnter()) {
                        ThreadPool.QueueUserWorkItem(o => {
                            asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback);
                        });
                    }
                };

                MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(SetupMethod);
                asyncHelper.OutstandingOperations.Increment();
                setupDispatcher.Execute(controllerContext.Controller, setupParametersArray);
                asyncHelper.OutstandingOperations.Decrement();
                return asyncResult;
            };

            AsyncCallback<object> endCallback = ar => {
                if (setupCompletedGate.TryEnter()) {
                    // the setup method did not complete before this callback executed
                    throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely);
                }

                // Get parameters for action method, then execute
                ParameterInfo[] completionParametersInfos = CompletionMethod.GetParameters();
                var rawCompletionParameterValues = from parameterInfo in completionParametersInfos
                                                   select ExtractParameterOrDefaultFromDictionary(parameterInfo, asyncHelper.Parameters);
                object[] completionParametersArray = rawCompletionParameterValues.ToArray();

                MethodDispatcher completionDispatcher = DispatcherCache.GetDispatcher(CompletionMethod);
                object actionReturnValue = completionDispatcher.Execute(controllerContext.Controller, completionParametersArray);
                return actionReturnValue;
            };

            // Set the timeout and go
            int timeout = asyncHelper.Timeout;
            return AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag);
        }
 public WrappedAsyncResult(BeginInvokeDelegate beginDelegate, EndInvokeDelegate <TResult> endDelegate, object tag)
 {
     _beginDelegateLockObj = new object();
     _endExecutedGate      = new SingleEntryGate();
     _handleCallbackGate   = new SingleEntryGate();
     _beginDelegate        = beginDelegate;
     _endDelegate          = endDelegate;
     _tag = tag;
 }
Esempio n. 3
0
 public WrappedTaskCompletionSource(int timeoutMs, string sourceId, Action <string> callback)
 {
     taskCompletionSource = new TaskCompletionSource <T1>();
     setResultGate        = new SingleEntryGate();
     this.sourceId        = sourceId;
     this.callback        = callback;
     if (timeoutMs > 0)
     {
         timer = new Timer(HandleTimeout, null, timeoutMs, Timeout.Infinite);
     }
 }
        public void TryEnterShouldBeTrueForFirstCallAndFalseForSubsequentCalls()
        {
            // Arrange
            SingleEntryGate gate = new SingleEntryGate();

            // Act
            bool firstCall = gate.TryEnter();
            bool secondCall = gate.TryEnter();
            bool thirdCall = gate.TryEnter();

            // Assert
            Assert.True(firstCall);
            Assert.False(secondCall);
            Assert.False(thirdCall);
        }
Esempio n. 5
0
        public void TryEnterShouldBeTrueForFirstCallAndFalseForSubsequentCalls()
        {
            // Arrange
            SingleEntryGate gate = new SingleEntryGate();

            // Act
            bool firstCall  = gate.TryEnter();
            bool secondCall = gate.TryEnter();
            bool thirdCall  = gate.TryEnter();

            // Assert
            Assert.True(firstCall);
            Assert.False(secondCall);
            Assert.False(thirdCall);
        }
Esempio n. 6
0
        public void TryEnter()
        {
            // Arrange
            SingleEntryGate gate = new SingleEntryGate();

            // Act
            bool firstCall  = gate.TryEnter();
            bool secondCall = gate.TryEnter();
            bool thirdCall  = gate.TryEnter();

            // Assert
            Assert.IsTrue(firstCall, "TryEnter() should return TRUE on first call.");
            Assert.IsFalse(secondCall, "TryEnter() should return FALSE on each subsequent call.");
            Assert.IsFalse(thirdCall, "TryEnter() should return FALSE on each subsequent call.");
        }
Esempio n. 7
0
 protected ControllerBase()
 {
     _executeGate = new SingleEntryGate();
 }