0

我有应该启动的特定数量的进程(C# .exe)。我如何根据它们的优先级启动它们。

我知道 Process.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();
4

1 回答 1

1

您可以使用进程文件名创建一个字典,然后使用 Linq 查询通过 ProcessPriorityClass 和 OrderBy 对它们进行排序。然后你只需执行它们迭代列表并使用值分配正确的优先级。

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();
    }
}
于 2013-01-13T19:30:30.133 回答