Beispiel #1
0
        /// <summary>
        /// Construct a ParallelWorkItemDispatcher
        /// </summary>
        /// <param name="levelOfParallelism">Number of workers to use</param>
        public ParallelWorkItemDispatcher(int levelOfParallelism)
        {
            _levelOfParallelism = levelOfParallelism;

            // Initialize WorkShifts
            Shifts = new WorkShift[]
            {
                _parallelShift,
                _nonParallelShift,
                _nonParallelSTAShift
            };

            foreach (var shift in Shifts)
            {
                shift.EndOfShift += OnEndOfShift;
            }

            // Set up queues for lazy initialization
            _parallelQueue = new Lazy <WorkItemQueue>(() =>
            {
                var parallelQueue = new WorkItemQueue("ParallelQueue");
                _parallelShift.AddQueue(parallelQueue);

                for (int i = 1; i <= _levelOfParallelism; i++)
                {
                    string name = string.Format("Worker#" + i.ToString());
                    _parallelShift.Assign(new TestWorker(parallelQueue, name, ApartmentState.MTA));
                }

                return(parallelQueue);
            });

            _parallelSTAQueue = new Lazy <WorkItemQueue>(() =>
            {
                var parallelSTAQueue = new WorkItemQueue("ParallelSTAQueue");
                _parallelShift.AddQueue(parallelSTAQueue);
                _parallelShift.Assign(new TestWorker(parallelSTAQueue, "Worker#STA", ApartmentState.STA));

                return(parallelSTAQueue);
            });

            _nonParallelQueue = new Lazy <WorkItemQueue>(() =>
            {
                var nonParallelQueue = new WorkItemQueue("NonParallelQueue");
                _nonParallelShift.AddQueue(nonParallelQueue);
                _nonParallelShift.Assign(new TestWorker(nonParallelQueue, "Worker#STA_NP", ApartmentState.MTA));

                return(nonParallelQueue);
            });

            _nonParallelSTAQueue = new Lazy <WorkItemQueue>(() =>
            {
                var nonParallelSTAQueue = new WorkItemQueue("NonParallelSTAQueue");
                _nonParallelSTAShift.AddQueue(nonParallelSTAQueue);
                _nonParallelSTAShift.Assign(new TestWorker(nonParallelSTAQueue, "Worker#NP_STA", ApartmentState.STA));

                return(nonParallelSTAQueue);
            });
        }
        /// <summary>
        /// Construct a ParallelWorkItemDispatcher
        /// </summary>
        /// <param name="levelOfParallelism">Number of workers to use</param>
        public ParallelWorkItemDispatcher(int levelOfParallelism)
        {
            // Create Shifts
            ParallelShift       = new WorkShift("Parallel");
            NonParallelShift    = new WorkShift("NonParallel");
            NonParallelSTAShift = new WorkShift("NonParallelSTA");

            foreach (var shift in Shifts)
            {
                shift.EndOfShift += OnEndOfShift;
            }

            // Assign queues to shifts
            ParallelShift.AddQueue(ParallelQueue);
            ParallelShift.AddQueue(ParallelSTAQueue);
            NonParallelShift.AddQueue(NonParallelQueue);
            NonParallelSTAShift.AddQueue(NonParallelSTAQueue);

            // Create workers and assign to shifts and queues
            // TODO: Avoid creating all the workers till needed
            for (int i = 1; i <= levelOfParallelism; i++)
            {
                string name = string.Format("Worker#" + i.ToString());
                ParallelShift.Assign(new TestWorker(ParallelQueue, name));
            }

            ParallelShift.Assign(new TestWorker(ParallelSTAQueue, "Worker#STA"));

            var worker = new TestWorker(NonParallelQueue, "Worker#STA_NP");

            worker.Busy += OnStartNonParallelWorkItem;
            NonParallelShift.Assign(worker);

            worker       = new TestWorker(NonParallelSTAQueue, "Worker#NP_STA");
            worker.Busy += OnStartNonParallelWorkItem;
            NonParallelSTAShift.Assign(worker);
        }
Beispiel #3
0
        /// <summary>
        /// Construct a ParallelWorkItemDispatcher
        /// </summary>
        /// <param name="levelOfParallelism">Number of workers to use</param>
        public ParallelWorkItemDispatcher(int levelOfParallelism)
        {
            _levelOfParallelism = levelOfParallelism;

            Shifts = new WorkShift[]
            {
                _parallelShift,
                _nonParallelShift,
#if !NETCF
                _nonParallelSTAShift
#endif
            };
            foreach (var shift in Shifts)
            {
                shift.EndOfShift += OnEndOfShift;
            }

            _parallelQueue = new Lazy <WorkItemQueue>(() =>
            {
                var parallelQueue = new WorkItemQueue("ParallelQueue");
                _parallelShift.AddQueue(parallelQueue);

                for (int i = 1; i <= _levelOfParallelism; i++)
                {
                    string name = string.Format("Worker#" + i.ToString());
#if NETCF
                    _parallelShift.Assign(new TestWorker(parallelQueue, name));
#else
                    _parallelShift.Assign(new TestWorker(parallelQueue, name, ApartmentState.MTA));
#endif
                }

                return(parallelQueue);
            });

#if !NETCF
            _parallelSTAQueue = new Lazy <WorkItemQueue>(() =>
            {
                var parallelSTAQueue = new WorkItemQueue("ParallelSTAQueue");
                _parallelShift.AddQueue(parallelSTAQueue);
                _parallelShift.Assign(new TestWorker(parallelSTAQueue, "Worker#STA", ApartmentState.STA));

                return(parallelSTAQueue);
            });
#endif

            _nonParallelQueue = new Lazy <WorkItemQueue>(() =>
            {
                var nonParallelQueue = new WorkItemQueue("NonParallelQueue");
                _nonParallelShift.AddQueue(nonParallelQueue);
#if NETCF
                _nonParallelShift.Assign(new TestWorker(nonParallelQueue, "Worker#NP"));
#else
                _nonParallelShift.Assign(new TestWorker(nonParallelQueue, "Worker#STA_NP", ApartmentState.MTA));
#endif

                return(nonParallelQueue);
            });

#if !NETCF
            _nonParallelSTAQueue = new Lazy <WorkItemQueue>(() =>
            {
                var nonParallelSTAQueue = new WorkItemQueue("NonParallelSTAQueue");
                _nonParallelSTAShift.AddQueue(nonParallelSTAQueue);
                _nonParallelSTAShift.Assign(new TestWorker(nonParallelSTAQueue, "Worker#NP_STA", ApartmentState.STA));

                return(nonParallelSTAQueue);
            });
#endif
        }