static void Single()
        {
            Task.Run(() => {
                Console.WriteLine("Enqueue elements...");
                for (int i = 1; i <= 20; i++)
                {
                    ObservableConcurrentQueue.Enqueue(i);
                    Thread.Sleep(100);
                }

                int item;

                Console.WriteLine("Peek & Dequeue 5 elements...");
                for (int i = 0; i < 5; i++)
                {
                    ObservableConcurrentQueue.TryPeek(out item);
                    Thread.Sleep(300);
                    ObservableConcurrentQueue.TryDequeue(out item);
                    Thread.Sleep(300);
                }

                ObservableConcurrentQueue.TryPeek(out item);
                Thread.Sleep(300);

                Console.WriteLine("Dequeue all elements...");
                while (ObservableConcurrentQueue.TryDequeue(out item))
                {
                    Thread.Sleep(300);
                }
            });
            Console.WriteLine("End. Press any key to exit...");
            Console.ReadKey(true);
        }
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void Main(string[] args)
        {
            var observableConcurrentQueue = new ObservableConcurrentQueue <int>();

            observableConcurrentQueue.ContentChanged += OnObservableConcurrentQueueContentChanged;
            var task = new Task(
                () =>
            {
                Console.WriteLine("Enqueue elements...");
                for (int i = 1; i <= 20; i++)
                {
                    observableConcurrentQueue.Enqueue(i);
                    Thread.Sleep(100);
                }

                int item;

                Console.WriteLine("Peek & Dequeue 5 elements...");
                for (int i = 0; i < 5; i++)
                {
                    observableConcurrentQueue.TryPeek(out item);
                    Thread.Sleep(300);
                    observableConcurrentQueue.TryDequeue(out item);
                    Thread.Sleep(300);
                }

                observableConcurrentQueue.TryPeek(out item);
                Thread.Sleep(300);

                Console.WriteLine("Dequeue all elements...");
                while (observableConcurrentQueue.TryDequeue(out item))
                {
                    Thread.Sleep(300);
                }
            });

            task.Start();
            Console.WriteLine("End. Press any key to exit...");
            Console.ReadKey(true);
        }
Example #3
0
        private static async Task TryItWithThreadSafeAsync()
        {
            var observableConcurrentQueue = new ObservableConcurrentQueue <int>();

            observableConcurrentQueue.ContentChanged += OnObservableConcurrentQueueContentChanged;
            await Task.Run(() =>

            {
                Console.WriteLine("Enqueue elements...");
                Parallel.For(1, 20, i => { observableConcurrentQueue.Enqueue(i); });

                int item;

                Console.WriteLine("Peek & Dequeue 5 elements...");
                Parallel.For(0, 5, i =>
                {
                    observableConcurrentQueue.TryPeek(out item);
                    Thread.Sleep(300);
                    observableConcurrentQueue.TryDequeue(out item);
                });

                Thread.Sleep(300);

                observableConcurrentQueue.TryPeek(out item);
                Thread.Sleep(300);

                Console.WriteLine("Dequeue all elements...");

                Parallel.For(1, 20, i =>
                {
                    while (observableConcurrentQueue.TryDequeue(out item))
                    {
                        // NO SLEEP, Force Concurrence
                        // Thread.Sleep(300);
                    }
                });
            }
                           );
        }
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void Main(string[] args)
        {
            var observableConcurrentQueue = new ObservableConcurrentQueue<int>();
            observableConcurrentQueue.ContentChanged += OnObservableConcurrentQueueContentChanged;
            var task = new Task(
                () =>
                    {
                        Console.WriteLine("Enqueue elements...");
                        for (int i = 1; i <= 20; i++)
                        {
                            observableConcurrentQueue.Enqueue(i);
                            Thread.Sleep(100);
                        }

                        int item;

                        Console.WriteLine("Peek & Dequeue 5 elements...");
                        for (int i = 0; i < 5; i++)
                        {
                            observableConcurrentQueue.TryPeek(out item);
                            Thread.Sleep(300);
                            observableConcurrentQueue.TryDequeue(out item);
                            Thread.Sleep(300);
                        }

                        observableConcurrentQueue.TryPeek(out item);
                        Thread.Sleep(300);

                        Console.WriteLine("Dequeue all elements...");
                        while (observableConcurrentQueue.TryDequeue(out item))
                        {
                            Thread.Sleep(300);
                        }
                    });
            task.Start();
            Console.WriteLine("End. Press any key to exit...");
            Console.ReadKey(true);
        }
Example #5
0
        private static void TryItWithoutThreadSafe()
        {
            var observableConcurrentQueue = new ObservableConcurrentQueue <int>();

            observableConcurrentQueue.ContentChanged += OnObservableConcurrentQueueContentChanged;
            var task = new Task(
                () =>
            {
                Console.WriteLine("Enqueue elements...");
                for (int i = 1; i <= 20; i++)
                {
                    observableConcurrentQueue.Enqueue(i);
                    Thread.Sleep(100);
                }

                int item;

                Console.WriteLine("Peek & Dequeue 5 elements...");
                for (int i = 0; i < 5; i++)
                {
                    observableConcurrentQueue.TryPeek(out item);
                    Thread.Sleep(300);
                    observableConcurrentQueue.TryDequeue(out item);
                    Thread.Sleep(300);
                }

                observableConcurrentQueue.TryPeek(out item);
                Thread.Sleep(300);

                Console.WriteLine("Dequeue all elements...");
                while (observableConcurrentQueue.TryDequeue(out item))
                {
                    Thread.Sleep(300);
                }
            });

            task.Start();
        }