public AsyncOperationContext[] Abort()
        {
            AsyncOperationContext[] retValue = null;
            bool setCloseHandle = false;

            lock (this.thisLock)
            {
                if (this.IsAborted)
                {
                    return(new AsyncOperationContext[] { });
                }
                else
                {
                    this.isAborted = true;
                }

                retValue = new AsyncOperationContext[this.activeOperations.Count];
                this.activeOperations.Values.CopyTo(retValue, 0);
                this.activeOperations.Clear();
                setCloseHandle = this.closeHandle != null;
            }

            if (setCloseHandle)
            {
                this.closeHandle.Set();
            }

            return(retValue);
        }
        public T Remove <T>(UniqueId operationId) where T : AsyncOperationContext
        {
            AsyncOperationContext context = null;
            bool setCloseHandle           = false;

            lock (this.thisLock)
            {
                if ((this.activeOperations.TryGetValue(operationId, out context)) &&
                    (context is T))
                {
                    this.activeOperations.Remove(operationId);
                    setCloseHandle = (this.closeHandle != null) && (this.activeOperations.Count == 0);
                }
                else
                {
                    context = null;
                }
            }

            if (setCloseHandle)
            {
                this.closeHandle.Set();
            }

            return(context as T);
        }
        public bool TryLookup(UniqueId operationId, out AsyncOperationContext context)
        {
            bool success;

            lock (this.thisLock)
            {
                success = this.activeOperations.TryGetValue(operationId, out context);
            }

            return(success);
        }
        public bool TryLookup <T>(UniqueId operationId, out T context) where T : AsyncOperationContext
        {
            AsyncOperationContext asyncContext = null;

            if (TryLookup(operationId, out asyncContext))
            {
                context = asyncContext as T;
                if (context != null)
                {
                    return(true);
                }
            }

            context = null;
            return(false);
        }
        public bool TryAdd(AsyncOperationContext context)
        {
            Fx.Assert(context != null, "The context must be non null.");

            lock (this.thisLock)
            {
                if (this.IsAborted || this.IsClosed)
                {
                    return(false);
                }
                if (this.activeOperations.ContainsKey(context.OperationId))
                {
                    return(false);
                }
                this.activeOperations.Add(context.OperationId, context);
            }

            return(true);
        }
        public bool TryRemoveUnique(object userState, out AsyncOperationContext context)
        {
            bool success        = false;
            bool setCloseHandle = false;

            context = null;

            lock (this.thisLock)
            {
                foreach (AsyncOperationContext value in this.activeOperations.Values)
                {
                    if (object.Equals(value.UserState, userState))
                    {
                        if (success)
                        {
                            success = false;
                            break;
                        }
                        else
                        {
                            context = value;
                            success = true;
                        }
                    }
                }

                if (success)
                {
                    this.activeOperations.Remove(context.OperationId);
                    setCloseHandle = (this.closeHandle != null) && (this.activeOperations.Count == 0);
                }
            }

            if (setCloseHandle)
            {
                this.closeHandle.Set();
            }

            return(success);
        }