// 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, BeginInvokeDelegate beginDelegate, EndInvokeDelegate <TResult> endDelegate, object tag, int timeout) { WrappedAsyncResult <TResult> result = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag); result.Begin(callback, state, timeout); return(result); }
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 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, 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 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 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 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); }