示例#1
0
        public void Test_QueueHandler_cancel_test()
        {

            List<string> result = new List<string>();

            QueueHandler<string> queueHandler = null;

            queueHandler = new QueueHandler<string>((p, token) => {

                if (token.IsCancellationRequested)
                    return;

                Thread.Sleep(10);

                if (token.IsCancellationRequested)
                    return;

                result.Add(p);

                if (result.Count == 20)
                    queueHandler.Dispose();

            });

            Action addAction = () =>
            {
                for (int i = 0; i < 10; ++i)
                    queueHandler.AddItem(i.ToString());
            };

            Thread[] threads = new Thread[4];

            for (int i = 0; i < threads.Count(); ++i)
                threads[i] = new Thread(() => addAction());

            foreach (var thread in threads)
                thread.Start();

            Thread.Sleep(2000);

            Assert.Equal(20, result.Count);


        }