Moo*_*oon 35 .net c# 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
});
Run Code Online (Sandbox Code Playgroud)
Rom*_*kov 54
正如其他人所提到的,您需要指定一个自定义调度程序来配合您的任务.不幸的是,没有合适的内置调度程序.
您可以使用Glenn链接到的ParallelExtensionsExtras,但如果您想要一些简单的东西可以直接粘贴到您的代码中,请尝试以下操作.使用这样:
Task.Factory.StartNew(() => {
// everything here will be executed in a thread whose priority is BelowNormal
}, null, TaskCreationOptions.None, PriorityScheduler.BelowNormal);
Run Code Online (Sandbox Code Playgroud)
代码:
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
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
MaximumConcurrencyLevel,GetScheduledTasks和TryExecuteTaskInline.net*_*rog 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;
}
}
Run Code Online (Sandbox Code Playgroud)
更改优先级时,请记住:为什么*不*更改ThreadPool(或任务)线程的优先级?
当你决定是否使用线程池时,这是"不要做"之一;-)
更多细节:http://msdn.microsoft.com/en-us/library/0ka9477y.aspx
所以答案是"不,你不能为Theads Pool中创建的线程指定特定的优先级"
至于一般的线程,我打赌你已经知道Thread.Priority属性了
要设置优先级Task,请查看Microsoft专家Stephen Toub在此MSDN博客文章中描述的自定义任务调度程序.有关更多详细信息,请不要错过他在第一句中提到的前两个帖子的链接.
对于你的问题,听起来你可能想看看QueuedTaskScheduler.
| 归档时间: |
|
| 查看次数: |
38157 次 |
| 最近记录: |