降低Task.Factory.StartNew线程的优先级

时间:2010-10-01 04:43:21

标签: c# .net multithreading .net-4.0 taskfactory

下面的代码将启动一个新线程来完成这项工作。有什么方法可以控制该线程的优先级吗?

Task.Factory.StartNew(() => {
    // everything here will be executed in a new thread.
    // I want to set the priority of this thread to BelowNormal
});

4 个答案:

答案 0 :(得分:50)

正如其他人所提到的,您需要指定一个自定义调度程序来配合您的任务。不幸的是,没有合适的内置调度程序。

您可以使用Glenn链接到的ParallelExtensionsExtras,但如果您想要一些简单的东西可以直接粘贴到您的代码中,请尝试以下操作。使用方式如下:

Task.Factory.StartNew(() => {
    // everything here will be executed in a thread whose priority is BelowNormal
}, null, TaskCreationOptions.None, PriorityScheduler.BelowNormal);

代码:

public class PriorityScheduler : TaskScheduler
{
    public static PriorityScheduler AboveNormal = new PriorityScheduler(ThreadPriority.AboveNormal);
    public static PriorityScheduler BelowNormal = new PriorityScheduler(ThreadPriority.BelowNormal);
    public static PriorityScheduler Lowest = new PriorityScheduler(ThreadPriority.Lowest);

    private BlockingCollection<Task> _tasks = new BlockingCollection<Task>();
    private Thread[] _threads;
    private ThreadPriority _priority;
    private readonly int _maximumConcurrencyLevel = Math.Max(1, Environment.ProcessorCount);

    public PriorityScheduler(ThreadPriority priority)
    {
        _priority = priority;
    }

    public override int MaximumConcurrencyLevel
    {
        get { return _maximumConcurrencyLevel; }
    }

    protected override IEnumerable<Task> GetScheduledTasks()
    {
        return _tasks;
    }

    protected override void QueueTask(Task task)
    {
        _tasks.Add(task);

        if (_threads == null)
        {
            _threads = new Thread[_maximumConcurrencyLevel];
            for (int i = 0; i < _threads.Length; i++)
            {
                int local = i;
                _threads[i] = new Thread(() =>
                {
                    foreach (Task t in _tasks.GetConsumingEnumerable())
                        base.TryExecuteTask(t);
                });
                _threads[i].Name = string.Format("PriorityScheduler: ", i);
                _threads[i].Priority = _priority;
                _threads[i].IsBackground = true;
                _threads[i].Start();
            }
        }
    }

    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
    {
        return false; // we might not want to execute task that should schedule as high or low priority inline
    }
}

注意:

  • 工作线程都是后台线程,因此不应使用此调度程序调度重要任务;只有在流程关闭时可以丢弃的那些
  • 改编自an implementation by Bnaya Eshet
  • 我不完全理解每一个覆盖;只需使用Bnaya对MaximumConcurrencyLevelGetScheduledTasksTryExecuteTaskInline的选择。

答案 1 :(得分:18)

可以在执行任务的实际方法内设置任务的线程优先级。但是,一旦完成以避免问题,不要忘记恢复优先级。

首先启动任务:

new TaskFactory().StartNew(StartTaskMethod);

然后设置线程优先级:

void StartTaskMethod()
{
    try
    {
        // Change the thread priority to the one required.
        Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

        // Execute the task logic.
        DoSomething();
    }
    finally
    {
        // Restore the thread default priority.
        Thread.CurrentThread.Priority = ThreadPriority.Normal;
    }
}

更改优先级时,请记住:Why *not* change the priority of a ThreadPool (or Task) thread?

答案 2 :(得分:6)

当您决定是否使用线程池时,这是“不要做”之一; - )

此处有更多详情:http://msdn.microsoft.com/en-us/library/0ka9477y.aspx

所以答案是“不,你不能为Theads Pool中创建的线程指定特定的优先级”

截至一般帖子,我打赌你已经知道Thread.Priority属性

答案 3 :(得分:5)

要使用Task设置优先级,请查看Microsoft专家Stephen Toub this MSDN blog post 中描述的自定义任务计划程序。有关详细信息,请不要错过他在第一句中提到的前两篇文章的链接。

对于您的问题,听起来您可能想要查看QueuedTaskScheduler