コード例 #1
0
        private Task runReadTask(OConcurrentQueue <int> q, CancellationTokenSource cTokenSource)
        {
            return(Task.Run(async() =>
            {
                Console.WriteLine("read task run");

                // хз но иногда WaitAsync вылетает по исключению (ловил вылет на отмене)
                try
                {
                    while (await q.WaitAsync(cTokenSource.Token))
                    {
                        //Console.WriteLine("queue not empty");
                        int n;
                        while (q.TryDequeue(out n))
                        {
                            Console.WriteLine("readed from pipe: " + n);
                            nn = n; // иммитация нагрузки
                        }
                    }
                    Console.WriteLine("false? last:" + nn);
                }
                catch
                {
                    Console.WriteLine("posible cancel?");
                }
            }));
        }
コード例 #2
0
        async public void TestPipeReadFirst()
        {
            var q            = new OConcurrentQueue <int>();
            var cTokenSource = new CancellationTokenSource();

            var readTask = Task.Run(async() =>
            {
                try
                {
                    while (await q.WaitAsync(cTokenSource.Token))
                    {
                        int n;
                        while (q.TryDequeue(out n))
                        {
                            Console.WriteLine("readed from pipe: " + n);
                        }
                    }
                }
                catch { }
            });

            var workTask1 = Task.Run(async() =>
            {
                for (var i = 0; i < 10; i++, i++)
                {
                    Console.WriteLine("put in pipe: " + i);
                    await Task.Delay(10);
                }
            });

            var killTask = runKillTask(1000, cTokenSource);

            //Task.WaitAll(readTask);
            await Task.WhenAll(readTask);

            //Assert.True(res == 30);
        }