public static IAsyncResult Begin <TResult, TState>(AsyncCallback callback, object callbackState, Func <AsyncCallback, object, TState, IAsyncResult> beginDelegate, Func <IAsyncResult, TState, TResult> endDelegate, TState invokeState, object tag = null, int timeout = -1, SynchronizationContext callbackSyncContext = null) { WrappedAsyncResult <TResult, TState> wrappedAsyncResult = new WrappedAsyncResult <TResult, TState>(beginDelegate, endDelegate, invokeState, tag, callbackSyncContext); wrappedAsyncResult.Begin(callback, callbackState, timeout); return(wrappedAsyncResult); }
public static IAsyncResult Begin <TResult>(AsyncCallback callback, object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate <TResult> endDelegate, object tag, int timeout) { WrappedAsyncResult <TResult> asyncResult = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag); asyncResult.Begin(callback, state, timeout); return(asyncResult); }
// kicks off an asynchronous operation public static IAsyncResult Begin <TResult>(AsyncCallback callback, object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate <TResult> endDelegate, object tag = null, int timeout = Timeout.Infinite) { WrappedAsyncResult <TResult> asyncResult = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag, callbackSyncContext: null); asyncResult.Begin(callback, state, timeout); return(asyncResult); }
public static IAsyncResult Begin <TResult>(AsyncCallback callback, object state, Func <AsyncCallback, object, IAsyncResult> beginDelegate, Func <IAsyncResult, TResult> endDelegate, object tag = null, int timeout = -1) { WrappedAsyncResult <TResult> wrappedAsyncResult = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag, null); wrappedAsyncResult.Begin(callback, state, timeout); return(wrappedAsyncResult); }
public static IAsyncResult BeginSynchronous <TResult, TState>(AsyncCallback callback, object callbackState, Func <IAsyncResult, TState, TResult> func, TState funcState, object tag) { Func <AsyncCallback, object, TState, IAsyncResult> completedBeginInvoke = CachedDelegates <TState> .CompletedBeginInvoke; WrappedAsyncResult <TResult, TState> wrappedAsyncResult = new WrappedAsyncResult <TResult, TState>(completedBeginInvoke, func, funcState, tag, null); wrappedAsyncResult.Begin(callback, callbackState, -1); return(wrappedAsyncResult); }
// wraps a synchronous operation in an asynchronous wrapper, but still completes synchronously public static IAsyncResult BeginSynchronous<TResult, TState>(AsyncCallback callback, object callbackState, EndInvokeDelegate<TState, TResult> func, TState funcState, object tag) { // Frequently called, so use static delegates // Inline delegates that take a generic argument from a generic method don't get cached by the compiler so use a field from a static generic class BeginInvokeDelegate<TState> beginDelegate = CachedDelegates<TState>.CompletedBeginInvoke; // Pass in the blocking function as the End() method WrappedAsyncResult<TResult, TState> asyncResult = new WrappedAsyncResult<TResult, TState>(beginDelegate, func, funcState, tag, callbackSyncContext: null); asyncResult.Begin(callback, callbackState, Timeout.Infinite); return asyncResult; }
public override IAsyncResult BeginExecute(Request request, AsyncCallback callback, object state) { var responseTask = new TaskCompletionSource <Response>(); BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { if (_commandService.WaitingRequests > ConfigurationSettings.MaxRequests) { responseTask.SetResult(Response.ServerTooBusy); return(CreateInnerAsyncResult(asyncCallback, asyncState)); } Type type; if (!BasicTypes.CommandTypes.TryGetValue(request.Header.GetIfKeyNotFound("Type"), out type)) { responseTask.SetResult(Response.UnknownType); return(CreateInnerAsyncResult(asyncCallback, asyncState)); } ICommand command; try { command = (ICommand)_serializer.Deserialize(request.Body, type); } catch (Exception) { responseTask.TrySetResult(Response.ParsingFailure); return(CreateInnerAsyncResult(asyncCallback, asyncState)); } var timeout = request.Header.GetIfKeyNotFound("Timeout", "0").ChangeIfError(0); var returnMode = (CommandReturnMode)request.Header.GetIfKeyNotFound("Mode", "1").ChangeIfError(1); var result = _commandService.Execute(command, returnMode, timeout); var message = _serializer.Serialize(result); responseTask.TrySetResult(new Response(200, message)); return(CreateInnerAsyncResult(asyncCallback, asyncState)); }; EndInvokeDelegate <Response> endDelegate = delegate { return(responseTask.Task.Result); }; return(WrappedAsyncResult <Response> .Begin( callback, state, beginDelegate, endDelegate, null, Timeout.Infinite)); }
public static WrappedAsyncResult <TResult> Cast(IAsyncResult result, object tag) { Precondition.Require(result, () => Error.ArgumentNull("result")); WrappedAsyncResult <TResult> castResult = (result as WrappedAsyncResult <TResult>); if (castResult != null && Object.Equals(castResult._tag, tag)) { return(castResult); } throw Error.InvalidAsyncResult("result"); }
public static IAsyncResult BeginSynchronous <TResult>(AsyncCallback callback, object state, Func <TResult> func, object tag) { BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { var result2 = new SimpleAsyncResult(asyncState); result2.MarkCompleted(true, asyncCallback); return(result2); }; EndInvokeDelegate <TResult> endDelegate = (_ => func()); var result = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag); result.Begin(callback, state, -1); return(result); }
public override IAsyncResult BeginExecute(Request request, AsyncCallback callback, object state) { var responseTask = new TaskCompletionSource <Response>(); BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { var typeName = request.Header.GetIfKeyNotFound("Type"); Type type; if (!_resultTypeMap.TryGetValue(typeName, out type)) { responseTask.SetResult(Response.UnknownType); return(CreateInnerAsyncResult(asyncCallback, asyncState)); } var traceId = request.Header.GetIfKeyNotFound("TraceId"); if (string.IsNullOrEmpty(traceId)) { var errorMessage = string.Format("Receive a empty traceId Reply Type({0}).", typeName); responseTask.SetResult(new Response(500, errorMessage)); return(CreateInnerAsyncResult(asyncCallback, asyncState)); } IResult result; try { result = (IResult)_serializer.Deserialize(request.Body, type); } catch (Exception) { responseTask.TrySetResult(Response.ParsingFailure); return(CreateInnerAsyncResult(asyncCallback, asyncState)); } _resultBus.Send(result, traceId); return(CreateInnerAsyncResult(asyncCallback, asyncState)); }; EndInvokeDelegate <Response> endDelegate = delegate { return(responseTask.Task.Result); }; return(WrappedAsyncResult <Response> .Begin( callback, state, beginDelegate, endDelegate, null, Timeout.Infinite)); }
public static WrappedAsyncResult<TResult> Cast(IAsyncResult asyncResult, object tag) { if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } WrappedAsyncResult<TResult> castResult = asyncResult as WrappedAsyncResult<TResult>; if (castResult != null && Equals(castResult._tag, tag)) { return castResult; } else { throw Error.AsyncCommon_InvalidAsyncResult("asyncResult"); } }
public static IAsyncResult BeginSynchronous <TResult>(AsyncCallback callback, object state, Func <TResult> func, object tag) { // Begin() doesn't perform any work on its own and returns immediately. BeginInvokeDelegate beginDelegate = (asyncCallback, asyncState) => { SimpleAsyncResult innerAsyncResult = new SimpleAsyncResult(asyncState); innerAsyncResult.MarkCompleted(true /* completedSynchronously */, asyncCallback); return(innerAsyncResult); }; // The End() method blocks. EndInvokeDelegate <TResult> endDelegate = _ => { return(func()); }; WrappedAsyncResult <TResult> asyncResult = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag); asyncResult.Begin(callback, state, Timeout.Infinite); return(asyncResult); }
public static IAsyncResult Begin <TResult, TState>( AsyncCallback callback, object callbackState, BeginInvokeDelegate <TState> beginDelegate, EndInvokeDelegate <TState, TResult> endDelegate, TState invokeState, object tag = null, int timeout = Timeout.Infinite, SynchronizationContext callbackSyncContext = null ) { WrappedAsyncResult <TResult, TState> asyncResult = new WrappedAsyncResult < TResult, TState >(beginDelegate, endDelegate, invokeState, tag, callbackSyncContext); asyncResult.Begin(callback, callbackState, timeout); return(asyncResult); }
public static IAsyncResult BeginSynchronous <TResult>(AsyncCallback callback, object state, Func <TResult> func, object tag) { BeginInvokeDelegate beginDelegate = (asyncCallback, asyncState) => { MvcAsyncResult inner = new MvcAsyncResult(asyncState); inner.MarkCompleted(true, asyncCallback); return(inner); }; EndInvokeDelegate <TResult> endDelegate = _ => { return(func()); }; WrappedAsyncResult <TResult> result = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag); result.Begin(callback, state, Timeout.Infinite); return(result); }
public static object Invoke(object untypedMethod, object[] arguments) { return(Helper.SimpleWrap <object>( delegate(ClrSyncManager manager) { var method = (Method)untypedMethod; if (method.ShortName == "BeginInvoke") { // extract the delegate arguments, and callback Debug.Assert(arguments.Length >= 3); var delegateArguments = new object[arguments.Length - 3]; var callback = (AsyncCallback)arguments[arguments.Length - 2]; var asyncState = arguments[arguments.Length - 1]; var @delegate = (global::System.Delegate)arguments[0]; global::System.Array.Copy(arguments, 1, delegateArguments, 0, delegateArguments.Length); // create the IAsyncResult that everyone shares var asyncResult = new WrappedAsyncResult(asyncState); // we wrap the actual invoke // so that CHESS gets control, as usual Helper.ThreadRoutineArg p = new Helper.ThreadRoutineArg(); p.s = new Original::Semaphore(0, 1); p.o = null; p.wcb = (o => { manager.SetMethodInfo("BeginInvoke(Begin)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_ACQUIRE); manager.CommitSyncVarAccess(); // synchronous call of the actual method object result = null; try { result = @delegate.DynamicInvoke(delegateArguments); // make sure to copy values of out params List <object> byRefResults = new List <object>(); var delegateParameters = @delegate.GetType().GetMethod("Invoke").GetParameters(); for (int i = 0; i < delegateParameters.Length; i++) { if (delegateParameters[i].ParameterType.IsByRef) { byRefResults.Add(delegateArguments[i]); } } asyncResult.SetByRefResults(byRefResults.ToArray()); } catch (Exception e) { // From ECMA: // Note: the callee can throw exceptions. // Any unhandled exception propagates to the caller via the EndInvoke method. asyncResult.CalleeThrown = e; } // this is the actual return results of the method, which becomes the return value // of the EndInvoke asyncResult.Result = result; manager.SetMethodInfo("BeginInvoke(Completed)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_RELEASE); // we set completed before the callback is done because // the callback may call EndInvoke. asyncResult.Completed = true; asyncResult.MyHandle.Set(); manager.CommitSyncVarAccess(); // From ECMA: The VES shall call this delegate when the value [the callback] // is computed or an exception has been raised indicating that the result will // not be available. // TODO: but how is the callback supposed to "know" whether or not an exception was raised? if (callback != null) { callback(asyncResult); } }); // create the wrapper WaitCallback call = Helper.ThreadCreateWrapper(manager); // store it aware for later use asyncResult.MyDelegate = call; // TODO: how do we insure this one is done without instrumentation? IAsyncResult invokeResult = call.BeginInvoke(p, null, null); // we squirrel this way because we have to match the above BeginInvoke // with the EndInvoke later asyncResult.RealAsyncResult = invokeResult; // let the asynch task proceed ChessTask child = manager.TaskFork(); manager.RegisterTaskSemaphore(child, p.s, false); manager.TaskResume(child); return asyncResult; } else if (method.ShortName == "EndInvoke") { WrappedAsyncResult asyncResult = (WrappedAsyncResult)arguments[arguments.Length - 1]; // wait for the BeginInvoke to complete while (true) { manager.SetMethodInfo("EndInvoke(Begin)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_ACQUIRE); if (!asyncResult.Completed) { manager.LocalBacktrack(); continue; } manager.CommitSyncVarAccess(); break; } // for good measure, do the EndInvoke, to match the BeginInvoke asyncResult.MyDelegate.EndInvoke(asyncResult.RealAsyncResult); manager.SetMethodInfo("EndInvoke(Completed)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_RELEASE); manager.CommitSyncVarAccess(); if (asyncResult.CalleeThrown != null) { throw asyncResult.CalleeThrown; } else { // copy the results back! global::System.Array.Copy(asyncResult.GetByRefResults(), 0, arguments, 1, asyncResult.GetByRefResults().Length); } return asyncResult.Result; } else { throw new InvalidOperationException("unexpected method: " + method.FullName); } }, delegate() { // TODO: no instrumentation return null; })); }
public static TResult End <TResult>(IAsyncResult asyncResult, object tag) { return(WrappedAsyncResult <TResult> .Cast(asyncResult, tag).End()); }
internal TableQuerySegment <TResult> EndExecuteQuerySegmentedInternal <TResult>(IAsyncResult asyncResult) { WrappedAsyncResult <TableQuerySegment <TResult> > wrappedAsyncResult = (WrappedAsyncResult <TableQuerySegment <TResult> >)asyncResult; return(wrappedAsyncResult != null?wrappedAsyncResult.Executor.EndExecute(asyncResult) : Executor.EndExecuteAsync <TableQuerySegment <TResult> >(asyncResult)); }
private static void OnAsyncReadDone(IAsyncResult result) { ReadData rd = (ReadData) result.AsyncState; IAsyncResult wrapped = new WrappedAsyncResult(result, rd.Buffer); rd.Callback(wrapped); }
internal static TableResult EndExecute(IAsyncResult asyncResult) { WrappedAsyncResult <TableResult> wrappedAsyncResult = (WrappedAsyncResult <TableResult>)asyncResult; return(wrappedAsyncResult != null?wrappedAsyncResult.Executor.EndExecute(asyncResult) : Executor.EndExecuteAsync <TableResult>(asyncResult)); }
public virtual Response EndExecute(IAsyncResult asyncResult) { return(WrappedAsyncResult <Response> .Cast(asyncResult, null).End()); }