設定指定程序的優先順序類別。 此值與程序中每個執行緒的優先權值共同決定每個執行緒的基礎優先權等級。
Syntax
BOOL SetPriorityClass(
[in] HANDLE hProcess,
[in] DWORD dwPriorityClass
);
Parameters
[in] hProcess
進程的句柄。
把手必須擁有 PROCESS_SET_INFORMATION 存取權限。 如需詳細資訊,請參閱 進程安全性和存取權限。
[in] dwPriorityClass
該程序的優先類別。 此參數可以是下列其中一個值。
傳回值
如果函式成功,則傳回值為非零值。
如果函數失敗,則傳回值為零。 若要取得擴充的錯誤資訊,請呼叫 GetLastError。
Remarks
每個執行緒都有由執行緒的優先權值及其程序優先權類別所決定的基本優先權等級。 系統會使用所有可執行執行緒的基礎優先權等級來決定哪個執行緒獲得下一個 CPU 時間切片。 SetThreadPriority 函數可設定執行緒相對於其程序優先權類別的基礎優先權等級。 如需更多資訊,請參閱 排程優先順序。
*_PRIORITY_CLASS 的值會影響程序的 CPU 排程優先順序。 對於執行背景工作的程序,如檔案 I/O、網路 I/O 或資料處理,僅調整 CPU 排程優先權是不夠的;即使是閒置的 CPU 優先程序,在使用磁碟和記憶體時也可能輕易干擾系統的回應速度。 執行背景工作的流程應利用 PROCESS_MODE_BACKGROUND_BEGIN 與 PROCESS_MODE_BACKGROUND_END 值調整資源排程優先順序;與使用者互動的程序不應使用 PROCESS_MODE_BACKGROUND_BEGIN。
如果程序處於背景處理模式,它建立的新執行緒也會處於背景處理模式。 當執行緒處於背景處理模式時,應盡量減少與其他執行緒共享關鍵區段、堆積及句柄等資源,否則可能會發生優先權反轉。 如果有執行緒以高優先權執行,處於背景處理模式的執行緒可能無法及時排程,但絕不會被餓死。
每個執行緒都可以使用 SetThreadPriority 獨立進入背景處理模式。 在程序中的執行緒呼叫 SetThreadPriority 進入背景處理模式後,請勿呼叫 SetPriorityClass 進入背景處理模式。 程序結束背景處理模式後,會重置該程序中的所有執行緒;然而,程序無法知道哪些執行緒已經處於背景處理模式。
Examples
以下範例示範程序背景模式的使用。
#include <windows.h>
#include <tchar.h>
int main( void )
{
DWORD dwError, dwPriClass;
if(!SetPriorityClass(GetCurrentProcess(), PROCESS_MODE_BACKGROUND_BEGIN))
{
dwError = GetLastError();
if( ERROR_PROCESS_MODE_ALREADY_BACKGROUND == dwError)
_tprintf(TEXT("Already in background mode\n"));
else _tprintf(TEXT("Failed to enter background mode (%d)\n"), dwError);
goto Cleanup;
}
// Display priority class
dwPriClass = GetPriorityClass(GetCurrentProcess());
_tprintf(TEXT("Current priority class is 0x%x\n"), dwPriClass);
//
// Perform background work
//
;
if(!SetPriorityClass(GetCurrentProcess(), PROCESS_MODE_BACKGROUND_END))
{
_tprintf(TEXT("Failed to end background mode (%d)\n"), GetLastError());
}
Cleanup:
// Clean up
;
return 0;
}
Requirements
| Requirement | 價值 |
|---|---|
| 最低支援客戶端 | Windows XP [桌面應用程式 |UWP 應用程式] |
| 最低支援伺服器數 | Windows Server 2003 [桌面應用程式 |UWP 應用程式] |
| 目標平台 | Windows |
| Header | processthreadsapi.h(包含 Windows Server 2003、Windows Vista、Windows 7、Windows Server 2008 及 Windows Server 2008 R2 上的 Windows.h) |
| 文件庫 | 內核 32.lib |
| DLL | Kernel32.dll |