public IAsyncResult BeginInvoke(AsyncCallback callback, object state)
        {
            AsyncCallback wrappedCallback = (callback != null)
                ? ar => _syncContext.Sync(() => callback(ar))
                : (AsyncCallback)null;

            return(_itemThunk.BeginInvoke(wrappedCallback, state));
        }
 public static void Sync(this SynchronizationContext syncContext, Action action)
 {
     syncContext.Sync(delegate
     {
         action();
         return(default(AsyncVoid));
     });
 }
            public TResult ExecuteEndDelegateOnce()
            {
                if (!_executeEndDelegateOnceEvent.Signal())
                {
                    throw new ArgumentException(MvcResources.AsyncCommon_ResultAlreadyExecuted, "asyncResult");
                }

                Timer timer = _timer;

                if (timer != null)
                {
                    lock (_timerLockObj) {
                        _timerDisposed = true;
                        timer.Dispose();
                    }
                }
                if (_timedOut)
                {
                    throw new TimeoutException();
                }

                return(_syncContext.Sync(() => EndCallback(_innerAsyncResult)));
            }
示例#4
0
        public static AsyncCallback WrapCallbackForSynchronizedExecution(
            AsyncCallback callback, SynchronizationContext context)
        {
            if (callback == null || context == null)
            {
                return(callback);
            }

            return(delegate(IAsyncResult result) {
                if (result.CompletedSynchronously)
                {
                    callback(result);
                }
                else
                {
                    context.Sync(() => callback(result));
                }
            });
        }
示例#5
0
        public static AsyncCallback WrapCallbackForSynchronizedExecution(AsyncCallback callback, SynchronizationContext syncContext)
        {
            if (callback == null || syncContext == null)
            {
                return(callback);
            }

            AsyncCallback newCallback = delegate(IAsyncResult asyncResult) {
                if (asyncResult.CompletedSynchronously)
                {
                    callback(asyncResult);
                }
                else
                {
                    // Only take the application lock if this request completed asynchronously,
                    // else we might end up in a deadlock situation.
                    syncContext.Sync(() => callback(asyncResult));
                }
            };

            return(newCallback);
        }
示例#6
0
 /// <summary>
 /// Executes a callback in the current synchronization context,
 /// which gives access to HttpContext and related items.
 /// </summary>
 /// <param name="action">The callback action.</param>
 public virtual void Sync(Action action)
 {
     _context.Sync(action);
 }
示例#7
0
 public void Post(Action d)
 {
     SynchronizationContext.Sync(d);
 }
 private void CallbackUsingSyncContext()
 {
     _callbackSyncContext.Sync(() => _originalCallback(this));
 }