Exemplo n.º 1
0
 public void Cancel()
 {
     if (Status == TaskStatus.WaitingToRun || Status == TaskStatus.Running)
     {
         Status = TaskStatus.Canceled;
         cancellation.Dispose();
     }
 }
Exemplo n.º 2
0
            IEnumerator DrainQueue(Queue <T> q, int frameCount)
            {
                lock (gate)
                {
                    readyDrainEnumerator = false; // use next queue.
                    running = false;
                }

                while (!cancelationToken.IsDisposed && frameCount-- != 0)
                {
                    yield return(null);
                }

                try
                {
                    if (q != null)
                    {
                        while (q.Count > 0 && !hasError)
                        {
                            if (cancelationToken.IsDisposed)
                            {
                                break;
                            }

                            lock (gate)
                            {
                                running = true;
                            }

                            var value = q.Dequeue();
                            observer.OnNext(value);

                            lock (gate)
                            {
                                running = false;
                            }
                        }

                        if (q.Count == 0)
                        {
                            pool.Return(q);
                        }
                    }

                    if (hasError)
                    {
                        if (!cancelationToken.IsDisposed)
                        {
                            cancelationToken.Dispose();

                            try { observer.OnError(error); } finally { Dispose(); }
                        }
                    }
                    else if (calledCompleted)
                    {
                        lock (gate)
                        {
                            // not self only
                            if (runningEnumeratorCount != 1)
                            {
                                yield break;
                            }
                        }

                        if (!cancelationToken.IsDisposed)
                        {
                            cancelationToken.Dispose();

                            try { observer.OnCompleted(); }
                            finally { Dispose(); }
                        }
                    }
                }
                finally
                {
                    lock (gate)
                    {
                        runningEnumeratorCount--;
                    }
                }
            }