Пример #1
0
        public void When_disposing_while_items_still_in_queue()
        {
            var          exceptions = new List <Exception>();
            var          numbers    = new List <int>();
            Action <int> work       = n =>
            {
                Thread.Sleep(500.Milliseconds());
                numbers.Add(n);
            };

            var pcq = new ProducerConsumerQueue <int>(work, 1);

            pcq.OnException += (sender, exception) => exceptions.Add(exception);

            pcq.Add(1);
            pcq.Add(2);
            pcq.Add(3);

            pcq.Dispose();

            pcq.Completion.Result.ShouldBeFalse();

            numbers.ShouldBe(new[] { 1 });
            exceptions.ShouldBeEmpty();

            Should.Throw <ObjectDisposedException>(() => pcq.Capacity.ShouldBe(-1))
            .Message.ShouldBe("The collection has been disposed.\r\nObject name: 'BlockingCollection'.");

            Should.Throw <ObjectDisposedException>(() => pcq.PendingCount.ShouldBe((uint)2))
            .Message.ShouldBe("The collection has been disposed.\r\nObject name: 'BlockingCollection'.");

            Should.Throw <ObjectDisposedException>(() => pcq.PendingItems.ShouldBe(new[] { 2, 3 }))
            .Message.ShouldBe("The collection has been disposed.\r\nObject name: 'BlockingCollection'.");
        }
Пример #2
0
 public void Stop()
 {
     try
     {
         listener.Stop();
         contextQueue.Dispose();
     }
     catch (Exception ex)
     {
         LogException("Stop", ex);
     }
     IsListening = false;
 }
        public void Dispose()
        {
            _cancellationTokenSource?.Cancel();

            if (OnSessionTermination != null)
            {
                var invocationList = OnSessionTermination?.GetInvocationList();

                foreach (var item in invocationList)
                {
                    OnSessionTermination -= (SessionTermination)item;
                }
            }

            _task?.Dispose();
            _producerConsumerQueue?.Dispose();
            _cancellationTokenSource?.Dispose();
        }
Пример #4
0
 public void Dispose()
 {
     _queue.Dispose();
 }