Пример #1
0
        public virtual void RegisterTask(Func <AsyncCallback, IAsyncResult> beginDelegate,
                                         Action <IAsyncResult> endDelegate)
        {
            Precondition.Require(beginDelegate, () => Error.ArgumentNull("beginDelegate"));
            Precondition.Require(endDelegate, () => Error.ArgumentNull("endDelegate"));

            AsyncCallback callback = ar => {
                lock (_taskLock)
                {
                }
                if (!ar.CompletedSynchronously)
                {
                    Sync(() => endDelegate(ar));
                    OutstandingOperations.Decrement();
                }
            };

            OutstandingOperations.Increment();

            IAsyncResult asyncResult;

            lock (_taskLock)
            {
                asyncResult = beginDelegate(callback);
            }
            if (asyncResult.CompletedSynchronously)
            {
                endDelegate(asyncResult);
                OutstandingOperations.Decrement();
            }
        }
Пример #2
0
        private IAsyncResult RegisterTaskInternal(Func <AsyncCallback, IAsyncResult> beginDelegate, AsyncCallback endDelegate)
        {
            AsyncCallback callback = ar => {
                Send(() => {
                    if (endDelegate != null)
                    {
                        endDelegate(ar);
                    }
                    OutstandingOperations.Decrement();
                });
            };

            OutstandingOperations.Increment();
            return(beginDelegate(callback));
        }