gpt4 book ai didi

c# - 如何限制进程的CPU使用率

转载 作者:行者123 更新时间:2023-12-01 21:38:15 24 4
gpt4 key购买 nike

我想创建一个程序,即使在计算机空闲时也可以限制进程的 CPU 使用率。

我编写了一个设置进程优先级的程序,但是如果计算机空闲,CPU使用率可以达到95%。该进程包含“元素”是我要限制的进程

private static readonly string[] RestrictedProcess = new[] { "element" }; 
static void ProcessChecker(object o)
{
List<Process> resProc = new List<Process>();
foreach(Process p in Process.GetProcesses())
{
string s = p.ProcessName;
foreach(string rp in RestrictedProcess)
{
s = s.ToLower();
if (s.Contains(rp))
resProc.Add(p);
}
}

foreach(Process p in resProc)
{
p.PriorityBoostEnabled = false;
p.PriorityClass = ProcessPriorityClass.Idle;
p.MaxWorkingSet = new IntPtr(20000000);
}

SetPowerConfig(resProc.Count > 0 ? PowerOption.GreenComputing : PowerOption.Balanced);
}

最佳答案

如果您要限制的程序不是您的,有多种选择:

  • 将进程优先级设置为空闲并且不限制CPU使用率,因为在任何情况下都应尽可能多地使用CPU。如果有什么有用的事情要做,让您的 CPU 始终运行 100% 就可以了。如果优先级为idle,则当其他程序需要 CPU 时,该特定进程的 CPU 使用率将会降低。
  • 如果您的系统是多核或多 CPU,那么您可能需要设置 processor affinity 。这将告诉您的程序仅使用您希望他使用的处理器。例如,如果你的程序是多线程的,并且能够100%消耗你的两个CPU,那么将他的affinity设置为只使用一个CPU。那么他的使用率将只有 50%。
  • 最差的选择,但实际上被 90% 的“CPU 限制程序”使用,您可以在网络上找到:测量进程的 CPU 使用率,并定期挂起和恢复它,直到达到 CPU 使用率为止转到您想要的值。

要暂停/恢复不属于您的进程,您必须使用 P/Invoke(这需要有权访问该进程,因此如果您使用的是 Windows Vista 或更高版本,请注意 UAC 的管理员权限):

/// <summary>
/// The process-specific access rights.
/// </summary>
[Flags]
public enum ProcessAccess : uint
{
/// <summary>
/// Required to terminate a process using TerminateProcess.
/// </summary>
Terminate = 0x1,

/// <summary>
/// Required to create a thread.
/// </summary>
CreateThread = 0x2,

/// <summary>
/// Undocumented.
/// </summary>
SetSessionId = 0x4,

/// <summary>
/// Required to perform an operation on the address space of a process (see VirtualProtectEx and WriteProcessMemory).
/// </summary>
VmOperation = 0x8,

/// <summary>
/// Required to read memory in a process using ReadProcessMemory.
/// </summary>
VmRead = 0x10,

/// <summary>
/// Required to write to memory in a process using WriteProcessMemory.
/// </summary>
VmWrite = 0x20,

/// <summary>
/// Required to duplicate a handle using DuplicateHandle.
/// </summary>
DupHandle = 0x40,

/// <summary>
/// Required to create a process.
/// </summary>
CreateProcess = 0x80,

/// <summary>
/// Required to set memory limits using SetProcessWorkingSetSize.
/// </summary>
SetQuota = 0x100,

/// <summary>
/// Required to set certain information about a process, such as its priority class (see SetPriorityClass).
/// </summary>
SetInformation = 0x200,

/// <summary>
/// Required to retrieve certain information about a process, such as its token, exit code, and priority class (see OpenProcessToken, GetExitCodeProcess, GetPriorityClass, and IsProcessInJob).
/// </summary>
QueryInformation = 0x400,

/// <summary>
/// Undocumented.
/// </summary>
SetPort = 0x800,

/// <summary>
/// Required to suspend or resume a process.
/// </summary>
SuspendResume = 0x800,

/// <summary>
/// Required to retrieve certain information about a process (see QueryFullProcessImageName). A handle that has the PROCESS_QUERY_INFORMATION access right is automatically granted PROCESS_QUERY_LIMITED_INFORMATION.
/// </summary>
QueryLimitedInformation = 0x1000,

/// <summary>
/// Required to wait for the process to terminate using the wait functions.
/// </summary>
Synchronize = 0x100000
}

[DllImport("ntdll.dll")]
internal static extern uint NtResumeProcess([In] IntPtr processHandle);

[DllImport("ntdll.dll")]
internal static extern uint NtSuspendProcess([In] IntPtr processHandle);

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr OpenProcess(
ProcessAccess desiredAccess,
bool inheritHandle,
int processId);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle([In] IntPtr handle);

public static void SuspendProcess(int processId)
{
IntPtr hProc = IntPtr.Zero;
try
{
// Gets the handle to the Process
hProc = OpenProcess(ProcessAccess.SuspendResume, false, processId);

if (hProc != IntPtr.Zero)
{
NtSuspendProcess(hProc);
}
}
finally
{
// Don't forget to close handle you created.
if (hProc != IntPtr.Zero)
{
CloseHandle(hProc);
}
}
}

关于c# - 如何限制进程的CPU使用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com