A task group is a group of tasks that you would like to execute at once. You can wait for all tasks in a task group to exit before your program continues.
Beispiel #1
0
/*
 *      /// <summary>
 *      /// Wait for all threads in the pool to complete.
 *      /// </summary>
 *      /// <param name="timeOutSeconds">How long to wait for all threads to complete.</param>
 *      public void Shutdown(long timeOutSeconds)
 *      {
 *          long start = DateTime.Now.Ticks;
 *          long current;
 *          long elapsed;
 *
 *          do
 *          {
 *              lock (this)
 *              {
 *                  if (this.activeTasks < 1)
 *                      return;
 *              }
 *              current = DateTime.Now.Ticks;
 *              elapsed = current - start;
 *              elapsed /= 10000; // to miliseconds
 *              elapsed /= 1000; // to seconds
 *              Thread.Sleep(100);
 *          } while (elapsed < timeOutSeconds);
 *      }
 */
        /// <summary>
        /// Create a new task group.
        /// </summary>
        /// <returns>The new task group.</returns>
        public TaskGroup CreateTaskGroup()
        {
            TaskGroup result = null;

            lock (this)
            {
                this.currentTaskGroup++;
                result = new TaskGroup(this.currentTaskGroup);
            }
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Process the specified task.  It will be processed either now,
        /// or queued to process on the thread pool.
        /// </summary>
        /// <param name="task">The task to process.</param>
        /// <param name="group">The group this task belongs to.</param>
        public void ProcessTask(IEngineTask task, TaskGroup group)
        {
            lock (this)
            {
                this.activeTasks++;
            }
            if (group != null)
            {
                group.TaskStarting();
            }
            PoolItem item = new PoolItem(this, task, group);

            ThreadPool.QueueUserWorkItem(item.ThreadPoolCallback);
        }
Beispiel #3
0
        /// <summary>
        /// Start the job, block until its done.
        /// </summary>
        public virtual void Process()
        {
            Object task;

            TaskGroup group = EngineConcurrency.Instance.CreateTaskGroup();

            this.totalTasks = LoadWorkload();
            int currentTask = 0;

            while ((task = RequestNextTask()) != null)
            {
                currentTask++;
                JobUnitContext context = new JobUnitContext();
                context.JobUnit    = task;
                context.Owner      = this;
                context.TaskNumber = currentTask;

                JobUnitWorker worker = new JobUnitWorker(context);
                EngineConcurrency.Instance.ProcessTask(worker, group);
            }

            group.WaitForComplete();
        }
Beispiel #4
0
 /// <summary>
 /// Construct a pool item.
 /// </summary>
 /// <param name="owner">The owner of this task.</param>
 /// <param name="task">The task to execute.</param>
 /// <param name="group">The group that this task belongs to.</param>
 public PoolItem(EngineConcurrency owner, IEngineTask task,  TaskGroup group)
 {
     this.owner = owner;
     this.task = task;
     this.group = group;
 }
/*
        /// <summary>
        /// Wait for all threads in the pool to complete.
        /// </summary>
        /// <param name="timeOutSeconds">How long to wait for all threads to complete.</param>
        public void Shutdown(long timeOutSeconds)
        {
            long start = DateTime.Now.Ticks;
            long current;
            long elapsed;

            do
            {
                lock (this)
                {
                    if (this.activeTasks < 1)
                        return;
                }
                current = DateTime.Now.Ticks;
                elapsed = current - start;
                elapsed /= 10000; // to miliseconds
                elapsed /= 1000; // to seconds
                Thread.Sleep(100);
            } while (elapsed < timeOutSeconds);
        }
        */
        /// <summary>
        /// Create a new task group.
        /// </summary>
        /// <returns>The new task group.</returns>
        public TaskGroup CreateTaskGroup()
        {
            TaskGroup result = null;
            lock (this)
            {
                this.currentTaskGroup++;
                result = new TaskGroup(this.currentTaskGroup);
                
            }
            return result;
        }
 /// <summary>
 /// Process the specified task.  It will be processed either now,
 /// or queued to process on the thread pool.
 /// </summary>
 /// <param name="task">The task to process.</param>
 /// <param name="group">The group this task belongs to.</param>
 public void ProcessTask(IEngineTask task, TaskGroup  group)
 {
     lock (this)
     {
         this.activeTasks++;
     }
     if( group!=null )
         group.TaskStarting();
     PoolItem item = new PoolItem(this, task, group);
     ThreadPool.QueueUserWorkItem(item.ThreadPoolCallback);
 }
Beispiel #7
0
 /// <summary>
 /// Construct a pool item.
 /// </summary>
 /// <param name="owner">The owner of this task.</param>
 /// <param name="task">The task to execute.</param>
 /// <param name="group">The group that this task belongs to.</param>
 public PoolItem(EngineConcurrency owner, IEngineTask task, TaskGroup group)
 {
     this.owner = owner;
     this.task  = task;
     this.group = group;
 }