如果您要限制的程序不是您的,有多种选择:
-
将进程优先级设置为
Idle
并且不限制CPU的使用因为在任何情况下都应该尽可能多地使用CPU。如果有什么有用的事情要做,让您的 CPU 始终运行 100% 就可以了。如果优先级是idle
,那么如果另一个程序需要 CPU,则该特定进程的 CPU 使用率将会减少。
- 如果您的系统是多核或多CPU,那么你可能想要设置处理器亲和力 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.processoraffinity.aspx。这将告诉您的程序仅使用您希望他使用的处理器。例如,如果你的程序是多线程的,并且能够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);
}
}
}