在进程启动之前为其分配优先级
本文关键字:分配 优先级 进程 启动 | 更新日期: 2023-09-27 17:52:36
我有一个应该启动的特定数量的进程(c# .exe)。如何根据它们的优先级启动它们
我知道这个过程。PriorityClass的东西,但它不是真的有用,因为它只在进程启动后分配优先级。
我这里有这段代码(还没有比较优先级),但它不起作用,因为进程没有运行,所以我不能给它们分配优先级:
Process process1 = new Process();
Process process2 = new Process();
Process process3 = new Process();
process1.StartInfo.FileName = "proc1";
process2.StartInfo.FileName = "proc2"'
process3.StartInfo.FileName = "proc3";
process1.PriorityClass = ProcessPriorityClass.AboveNormal;
process2.PriorityClass = ProcessPriorityClass.BelowNormal;
process3.PriorityClass = ProcessPriorityClass.High;
process2.Start();
process2.WaitForExit();
process1.Start();
process1.WaitForExit();
process3.Start();
您可以创建一个包含进程文件名的Dictionary,然后使用Linq查询通过ProcessPriorityClass和OrderBy对它们进行排序。然后执行它们,迭代列表并使用is值分配正确的优先级。
public void StartProcessesByPriority(Dictionary<String, ProcessPriorityClass> values)
{
List<KeyValuePair<String, ProcessPriorityClass>> valuesList = values.ToList();
valuesList.Sort
(
delegate(KeyValuePair<String, ProcessPriorityClass> left, KeyValuePair<String, ProcessPriorityClass> right)
{
return left.Value.CompareTo(right.Value);
}
);
foreach (KeyValuePair<String, ProcessPriorityClass> pair in valuesList)
{
Process process = new Process();
process.StartInfo.FileName = pair.Key;
process.Start();
process.PriorityClass = pair.Value;
process.WaitForExit();
}
}