|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +class ProdoucerConsumer |
| 8 | +{ |
| 9 | + private readonly int inputs = 2000; |
| 10 | + private CancellationTokenSource ct; |
| 11 | + private BlockingCollection<int> numbers; |
| 12 | + private int capacity; |
| 13 | + |
| 14 | + public ProdoucerConsumer(int capa) |
| 15 | + { |
| 16 | + ct = new CancellationTokenSource(); |
| 17 | + capacity = capa; |
| 18 | + numbers = new BlockingCollection<int>(capacity); |
| 19 | + } |
| 20 | + |
| 21 | + public void Shutdown() |
| 22 | + { |
| 23 | + ct.Cancel(); |
| 24 | + } |
| 25 | + |
| 26 | + public void Run() |
| 27 | + { |
| 28 | + // Wait for the tasks to complete execution |
| 29 | + Task.WaitAll(Task.Run(() => Consumer()), |
| 30 | + Task.Run(() => Producer())); |
| 31 | + |
| 32 | + ct.Dispose(); |
| 33 | + Console.WriteLine("Press the Enter key to exit."); |
| 34 | + Console.ReadLine(); |
| 35 | + } |
| 36 | + |
| 37 | + void Consumer() |
| 38 | + { |
| 39 | + while (!numbers.IsCompleted) |
| 40 | + { |
| 41 | + int nextItem = 0; |
| 42 | + try |
| 43 | + { |
| 44 | + if (!numbers.TryTake(out nextItem, 0, ct.Token)) |
| 45 | + Console.WriteLine(" Take Blocked"); |
| 46 | + else |
| 47 | + Console.WriteLine(" Take:{0}", nextItem); |
| 48 | + } |
| 49 | + catch (OperationCanceledException) |
| 50 | + { |
| 51 | + Console.WriteLine("Taking canceled."); |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + // REAMRK |
| 56 | + // : consumers work here! |
| 57 | + // Slow down consumer just a little to cause |
| 58 | + // collection to fill up faster, and lead to "AddBlocked" |
| 59 | + Thread.SpinWait(500000); |
| 60 | + } |
| 61 | + |
| 62 | + Console.WriteLine("\r\nNo more items to take."); |
| 63 | + } |
| 64 | + |
| 65 | + void Producer() |
| 66 | + { |
| 67 | + int itemToAdd = 0; |
| 68 | + bool success = false; |
| 69 | + |
| 70 | + do |
| 71 | + { |
| 72 | + // Cancellation causes OCE. We know how to handle it. |
| 73 | + try |
| 74 | + { |
| 75 | + // A shorter timeout causes more failures. |
| 76 | + success = numbers.TryAdd(itemToAdd, 2, ct.Token); |
| 77 | + } |
| 78 | + catch (OperationCanceledException) |
| 79 | + { |
| 80 | + Console.WriteLine("Add loop canceled."); |
| 81 | + // Let other threads know we're done in case |
| 82 | + // they aren't monitoring the cancellation token. |
| 83 | + numbers.CompleteAdding(); |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + if (success) |
| 88 | + { |
| 89 | + Console.WriteLine(" Add:{0}", itemToAdd); |
| 90 | + itemToAdd++; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + Console.Write(" AddBlocked:{0} Count = {1}", itemToAdd.ToString(), numbers.Count); |
| 95 | + // Don't increment nextItem. Try again on next iteration. |
| 96 | + |
| 97 | + //Do something else useful instead. |
| 98 | + // UpdateProgress(itemToAdd); |
| 99 | + } |
| 100 | + } while (itemToAdd < inputs); |
| 101 | + |
| 102 | + // No lock required here because only one producer. |
| 103 | + numbers.CompleteAdding(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class ProgramWithCancellation |
| 108 | +{ |
| 109 | + static void Main() |
| 110 | + { |
| 111 | + var pc = new ProdoucerConsumer(); |
| 112 | + pc.Run(); |
| 113 | + } |
| 114 | +} |
0 commit comments