以下是 CWinThread::SetThreadPriority 方法的简要信息:
BOOL SetThreadPriority(int nPriority);
- nPriority:线程的优先级,可以是以下之一的值:
- THREAD_PRIORITY_IDLE
- THREAD_PRIORITY_LOWEST
- THREAD_PRIORITY_BELOW_NORMAL
- THREAD_PRIORITY_NORMAL
- THREAD_PRIORITY_ABOVE_NORMAL
- THREAD_PRIORITY_HIGHEST
- THREAD_PRIORITY_TIME_CRITICAL
该方法返回一个 BOOL 类型,指示是否成功设置线程的优先级。如果成功,返回 TRUE;否则返回 FALSE。
示例代码:
CWinThread* pThread = AfxGetThread();
if (pThread != nullptr)
{
// 设置线程的优先级为最高
BOOL bSuccess = pThread->SetThreadPriority(THREAD_PRIORITY_HIGHEST);
if (bSuccess)
{
TRACE(_T("Thread priority set to highest.\n"));
}
else
{
TRACE(_T("Failed to set thread priority.\n"));
}
}
在这个示例中,AfxGetThread 返回当前线程的 CWinThread 指针,然后通过 SetThreadPriority 方法将线程的优先级设置为最高。你可以根据实际需求选择合适的优先级。
转载请注明出处:http://www.zyzy.cn/article/detail/23247/MFC/CWinThread