public void Signal() { // Arrange object theValue = null; ContinuationListener listener = new ContinuationListener(); // Act listener.Signal(); listener.SetContinuation(() => { theValue = 42; }); // Assert Assert.AreEqual(42, theValue); }
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); SingleFireEvent setupCompletedEvent = new SingleFireEvent(); ContinuationListener listener = new ContinuationListener(); object theDelegate = null; BeginInvokeCallback beginCallback = (innerCallback, innerState) => { ManualAsyncResult asyncResult = new ManualAsyncResult() { AsyncState = innerState }; // Get parameters for async setup method, then execute ParameterInfo[] setupParametersInfos = ActionMethod.GetParameters(); var rawSetupParameterValues = from parameterInfo in setupParametersInfos select ExtractParameterFromDictionary(parameterInfo, parameters, ActionMethod); object[] setupParametersArray = rawSetupParameterValues.ToArray(); // to simplify the logic, force an asynchronous callback asyncHelper.OutstandingOperations.Completed += delegate { if (setupCompletedEvent.Signal()) { listener.SetContinuation(() => { ThreadPool.QueueUserWorkItem(o => { asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback); }); }); } }; MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(ActionMethod); asyncHelper.OutstandingOperations.Increment(); object returnedDelegate = setupDispatcher.Execute(controllerContext.Controller, setupParametersArray); ValidateDelegateNotNull(returnedDelegate, ActionMethod); asyncHelper.OutstandingOperations.Decrement(); Thread.VolatileWrite(ref theDelegate, returnedDelegate); listener.Signal(); return asyncResult; }; AsyncCallback<object> endCallback = ar => { if (setupCompletedEvent.Signal()) { // the setup method did not complete before this callback executed throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely); } object returnedDelegate = Thread.VolatileRead(ref theDelegate); MethodInfo invokeMethod = returnedDelegate.GetType().GetMethod("Invoke", Type.EmptyTypes); MethodDispatcher invokeDispatcher = DispatcherCache.GetDispatcher(invokeMethod); object actionReturnValue = invokeDispatcher.Execute(returnedDelegate, _emptyParameters); return actionReturnValue; }; // Set the timeout and go int timeout = asyncHelper.Timeout; return AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag); }
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); SingleFireEvent setupCompletedEvent = new SingleFireEvent(); ContinuationListener listener = new ContinuationListener(); object theDelegate = null; BeginInvokeCallback beginCallback = (innerCallback, innerState) => { ManualAsyncResult asyncResult = new ManualAsyncResult() { AsyncState = innerState }; // Get parameters for async setup method, then execute ParameterInfo[] setupParametersInfos = ActionMethod.GetParameters(); var rawSetupParameterValues = from parameterInfo in setupParametersInfos select ExtractParameterFromDictionary(parameterInfo, parameters, ActionMethod); object[] setupParametersArray = rawSetupParameterValues.ToArray(); // to simplify the logic, force an asynchronous callback asyncHelper.OutstandingOperations.Completed += delegate { if (setupCompletedEvent.Signal()) { listener.SetContinuation(() => { ThreadPool.QueueUserWorkItem(o => { asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback); }); }); } }; MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(ActionMethod); asyncHelper.OutstandingOperations.Increment(); object returnedDelegate = setupDispatcher.Execute(controllerContext.Controller, setupParametersArray); ValidateDelegateNotNull(returnedDelegate, ActionMethod); asyncHelper.OutstandingOperations.Decrement(); Thread.VolatileWrite(ref theDelegate, returnedDelegate); listener.Signal(); return(asyncResult); }; AsyncCallback <object> endCallback = ar => { if (setupCompletedEvent.Signal()) { // the setup method did not complete before this callback executed throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely); } object returnedDelegate = Thread.VolatileRead(ref theDelegate); MethodInfo invokeMethod = returnedDelegate.GetType().GetMethod("Invoke", Type.EmptyTypes); MethodDispatcher invokeDispatcher = DispatcherCache.GetDispatcher(invokeMethod); object actionReturnValue = invokeDispatcher.Execute(returnedDelegate, _emptyParameters); return(actionReturnValue); }; // Set the timeout and go int timeout = asyncHelper.Timeout; return(AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag)); }