/// <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, _nonParallelSTAShift };
            foreach (var shift in Shifts)
                shift.EndOfShift += OnEndOfShift;
        }
Ejemplo n.º 2
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,
                _nonParallelSTAShift
            };
            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());
                    _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)
        {
            _levelOfParallelism = levelOfParallelism;

            Shifts = new WorkShift[] { _parallelShift, _nonParallelShift, _nonParallelSTAShift };
            foreach (var shift in Shifts)
            {
                shift.EndOfShift += OnEndOfShift;
            }
        }
Ejemplo n.º 4
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,
                _nonParallelSTAShift
            };
            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());
                    _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;
            });
        }
Ejemplo n.º 5
0
        private void OnEndOfShift(WorkShift endingShift)
        {
            ShiftFinished?.Invoke(endingShift);

            WorkShift nextShift = null;

            while (true)
            {
                // Shift has ended but all work may not yet be done
                while (_topLevelWorkItem.State != WorkItemState.Complete)
                {
                    // This will return null if all queues are empty.
                    nextShift = SelectNextShift();
                    if (nextShift != null)
                    {
                        ShiftStarting?.Invoke(nextShift);
                        nextShift.Start();
                        return;
                    }
                }

                // If the shift has ended for an isolated queue, restore
                // the queues and keep trying. Otherwise, we are done.
                if (_isolationLevel > 0)
                {
                    RestoreQueues();
                }
                else
                {
                    break;
                }
            }

            // All done - shutdown all shifts
            foreach (var shift in Shifts)
            {
                shift.ShutDown();
            }
        }
Ejemplo n.º 6
0
        /// <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);
        }
 public void CreateShift()
 {
     _shift = new WorkShift("dummy");
 }
Ejemplo n.º 8
0
 public void CreateShift()
 {
     _shift = new WorkShift("dummy");
 }