示例#1
0
        /// <summary>
        /// Schedules a task to the ThreadPool.
        /// </summary>
        /// <param name="task">The task to schedule.</param>
        protected internal override void QueueTask(Task task)
        {
            if (TaskTrace.Enabled)
            {
                Task currentTask  = Task.InternalCurrent;
                Task creatingTask = task.m_parent;

                TaskTrace.TaskScheduled(this.Id, currentTask == null ? 0 : currentTask.Id,
                                        task.Id, creatingTask == null ? 0 : creatingTask.Id,
                                        (int)task.Options);
            }

            if ((task.Options & TaskCreationOptions.LongRunning) != 0)
            {
                // Run LongRunning tasks on their own dedicated thread.
                RuntimeThread thread = RuntimeThread.Create(s_longRunningThreadWork, 0);
                thread.IsBackground = true; // Keep this thread from blocking process shutdown
                thread.Start(task);
            }
            else
            {
                // Normal handling for non-LongRunning tasks.
                bool preferLocal = (task.Options & TaskCreationOptions.PreferFairness) == 0;
                ThreadPool.UnsafeQueueUserWorkItemInternal(task, preferLocal);
            }
        }
        /// <summary>
        /// Schedules a task to the ThreadPool.
        /// </summary>
        /// <param name="task">The task to schedule.</param>
        protected internal override void QueueTask(Task task)
        {
            if (TaskTrace.Enabled)
            {
                Task currentTask  = Task.InternalCurrent;
                Task creatingTask = task.m_parent;

                TaskTrace.TaskScheduled(this.Id, currentTask == null ? 0 : currentTask.Id,
                                        task.Id, creatingTask == null ? 0 : creatingTask.Id,
                                        (int)task.Options);
            }

            if ((task.Options & TaskCreationOptions.LongRunning) != 0)
            {
                ThreadPool.QueueLongRunningWork(() => task.ExecuteEntry(false));
            }
            else
            {
                // Normal handling for non-LongRunning tasks.
                bool forceToGlobalQueue = ((task.Options & TaskCreationOptions.PreferFairness) != 0);
                ThreadPool.UnsafeQueueCustomWorkItem(task, forceToGlobalQueue);
            }
        }