コード例 #1
0
        public async Task TestParallelThrottling()
        {
            List <int>          list = Enumerable.Range(0, 100).ToList <int>();
            IReadOnlyList <int> col  = new ReadOnlyCollection <int>(list);

            var count           = 0;
            var concurrentCount = new ConcurrentStack <int>();

            col.ParallelForEachAsync <int>(10, async x =>
            {
                concurrentCount.Push((Interlocked.Increment(ref count)));
                await Task.Delay(1000);
                concurrentCount.Push((Interlocked.Decrement(ref count)));
            });

            Assert.AreEqual(concurrentCount.Max(), 10);
        }