前言
关于设置C#线程/进程的优先级。
一、设置线程的优先级
线程的优先级共有5个级别,如下,默认为ThreadPriority.Normal
#region 程序集 System.Threading.Thread, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.12\ref\net6.0\System.Threading.Thread.dll
#endregion
namespace System.Threading
{
//
// 摘要:
// Specifies the scheduling priority of a System.Threading.Thread.
public enum ThreadPriority
{
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with any other priority.
Lowest = 0,
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with Normal priority
// and before those with Lowest priority.
BelowNormal = 1,
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with AboveNormal priority
// and before those with BelowNormal priority. Threads have Normal priority by default.
Normal = 2,
//
// 摘要:
// The System.Threading.Thread can be scheduled after threads with Highest priority
// and before those with Normal priority.
AboveNormal = 3,
//
// 摘要:
// The System.Threading.Thread can be scheduled before threads with any other priority.
Highest = 4
}
}
设置线程优先级为最高
public void SetThreadPriority()
{
Thread thread = new Thread(findmin);
thread.IsBackground = true;
thread.Priority= ThreadPriority.Highest;
thread.Start();
}
要注意:优先级是相对的,最佳方式是只设置一个最需要实时性的线程为最高优先级
二、设置进程优先级
线程的优先级共有5个级别,如下,默认为ProcessPriorityClass.Normal
#region 程序集 System.Diagnostics.Process, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.12\ref\net6.0\System.Diagnostics.Process.dll
#endregion
namespace System.Diagnostics
{
//
// 摘要:
// Indicates the priority that the system associates with a process. This value,
// together with the priority value of each thread of the process, determines each
// thread's base priority level.
public enum ProcessPriorityClass
{
//
// 摘要:
// Specifies that the process has no special scheduling needs.
Normal = 32,
//
// 摘要:
// Specifies that the threads of this process run only when the system is idle,
// such as a screen saver. The threads of the process are preempted by the threads
// of any process running in a higher priority class. This priority class is inherited
// by child processes.
Idle = 64,
//
// 摘要:
// Specifies that the process performs time-critical tasks that must be executed
// immediately, such as the Task List dialog, which must respond quickly when called
// by the user, regardless of the load on the operating system. The threads of the
// process preempt the threads of normal or idle priority class processes.
// Use extreme care when specifying High for the process's priority class, because
// a high priority class application can use nearly all available processor time.
High = 128,
//
// 摘要:
// Specifies that the process has the highest possible priority.
// The threads of a process with RealTime priority preempt the threads of all other
// processes, including operating system processes performing important tasks. Thus,
// a RealTime priority process that executes for more than a very brief interval
// can cause disk caches not to flush or cause the mouse to be unresponsive.
RealTime = 256,
//
// 摘要:
// Specifies that the process has priority above Idle but below Normal.
BelowNormal = 16384,
//
// 摘要:
// Specifies that the process has priority higher than Normal but lower than High.
AboveNormal = 32768
}
}
设置进程优先级为最高
public void SetProcessPriority()
{
//using System.Diagnostics;
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
}
要注意:优先级是相对的,最佳方式是只设置一个最需要实时性的进程为最高优先级
三、如何查看进程优先级
四、提高计划优先级
increase-scheduling-priority:官网解释
扫描二维码关注公众号,回复:
16638359 查看本文章
打开控制面板,选择管理工具
打开本地安全策略
选择本地策略-用户权限分配-提高计划优先级
添加用户并检测名称
总结
不积硅步,何以至千里