Recipe 8.17. Boosting the Priority of a Process ProgrammaticallyProblemYou want a specific program to run at a higher priority than most of the other processes on the machine to help it complete a time-critical task. SolutionUse the ProcessStartInfo and Process classes to launch the program and then adjust it to a higher priority like this: // Run recursive dir operation on the c:\ drive ProcessStartInfo psi = new ProcessStartInfo("cmd.exe","/C\"dir c:\\ /S\""); // Don't show the window. psi.CreateNoWindow = true; // Start the process. Process p = Process.Start(psi); // Raise the process priority to AboveNormal. p.PriorityClass = ProcessPriorityClass.AboveNormal; DiscussionThe Process.PriorityClass property takes one of the ProcessPriorityClass enumeration values to indicate what priority the process should have. The enumeration values and descriptions are listed in Table 8-5.
Setting the priority above AboveNormal should be done in only very specialized situations for short durations of time, as it can starve other processes on the machine. The operating system uses the priority level to determine scheduling for the processors, and if one process is set to a higher priority for a long time, the processes with lesser priority may never finish. See AlsoSee the "Process Class," "ProcessStartInfo Class," and "ProcessPriorityClass Enumeration" topics in the MSDN documentation. |