Пример #1
0
        public void WaitAll()
        {
            if (_storage == null)
            {
                return;
            }
            if (!_isStorageRing)
            {
                StorageAsTask.Wait();
            }
            var handles = new WaitHandle[StorageAsRing.Count];
            int i       = 0;

            foreach (Task task in StorageAsRing)
            {
                if (task == null)
                {
                    throw new ArgumentNullException();
                }
                if (!TaskContinuationManager.ScheduleAntecedent(task))
                {
                    task.Schedule();
                }
                //BUG?: we need to handle completed tasks
                handles[i++] = task.CompletionHandle;
            }
            WaitHandle.WaitAll(handles);
        }
Пример #2
0
        public Task WaitAny()
        {
            if (_storage == null)
            {
                return(Task.Completed());
            }
            if (!_isStorageRing)
            {
                StorageAsTask.Wait();
            }
            var handles = new WaitHandle[StorageAsRing.Count];
            int i       = 0;

            foreach (Task task in StorageAsRing)
            {
                if (task == null)
                {
                    throw new ArgumentNullException();
                }
                if (task.IsComplete)
                {
                    return(task);
                }
                if (!TaskContinuationManager.ScheduleAntecedent(task))
                {
                    task.Schedule();
                }
                //BUG?: we need to handle completed tasks
                handles[i++] = task.CompletionHandle;
            }
            var index = WaitHandle.WaitAny(handles);

            return((Task)StorageAsRing.FindAtIndex(index)
                   .Value);
        }
Пример #3
0
        /// <summary>
        /// Note that a canceled task will raise an OperationCanceledException wrapped in an AggregateException,
        /// whereas a canceled wait will raise an unwrapped OperationCanceledException.
        /// </summary>
        /// <param name="millisecondsTimeout"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public bool Wait(int millisecondsTimeout = -1, CancellationToken cancellationToken = null)
        {
            if (cancellationToken == null)
            {
                cancellationToken = CancellationToken.None;
            }
            //todo: ThrowIfDisposed();?
            if (IsComplete)
            {
                return(true);
            }
            switch (_status)
            {
            case TaskStatus.WaitingForAcivation:
                Schedule();
                break;

            case TaskStatus.WaitingForAnticedent:
                TaskContinuationManager.ScheduleAntecedent(this);
                break;
            }
            bool waitSuccess = _completionHandle.WaitOne(millisecondsTimeout, false);

            if (_exceptionRecord != null)
            {
                throw _exceptionRecord.Exceptions;
            }
            return(waitSuccess);
        }