我試圖改變線程的優先級,但我沒有運氣。我從GetLastError函數得到一個錯誤的句柄錯誤(類型6)。我雖然native_handle()返回線程的句柄?在Windows中更改提升線程優先級
任何人都知道如何做到這一點?
void baseThread::applyPriority(uint8 priority)
{
#ifdef WIN32
if (!m_pThread)
return;
BOOL res;
HANDLE th = m_pThread->native_handle();
switch (priority)
{
case REALTIME : res = SetPriorityClass(th, REALTIME_PRIORITY_CLASS); break;
case HIGH : res = SetPriorityClass(th, HIGH_PRIORITY_CLASS); break;
case ABOVE_NORMAL : res = SetPriorityClass(th, ABOVE_NORMAL_PRIORITY_CLASS); break;
case NORMAL : res = SetPriorityClass(th, NORMAL_PRIORITY_CLASS); break;
case BELOW_NORMAL : res = SetPriorityClass(th, BELOW_NORMAL_PRIORITY_CLASS); break;
case IDLE : res = SetPriorityClass(th, IDLE_PRIORITY_CLASS); break;
}
if (res == FALSE)
{
int err = GetLastError();
}
#endif
}
編輯:最終代碼:
void baseThread::applyPriority(uint8 priority)
{
#ifdef WIN32
if (!m_pThread)
return;
BOOL res;
HANDLE th = m_pThread->native_handle();
switch (priority)
{
case REALTIME : res = SetThreadPriority(th, THREAD_PRIORITY_TIME_CRITICAL); break;
case HIGH : res = SetThreadPriority(th, THREAD_PRIORITY_HIGHEST); break;
case ABOVE_NORMAL : res = SetThreadPriority(th, THREAD_PRIORITY_ABOVE_NORMAL); break;
case NORMAL : res = SetThreadPriority(th, THREAD_PRIORITY_NORMAL); break;
case BELOW_NORMAL : res = SetThreadPriority(th, THREAD_PRIORITY_BELOW_NORMAL); break;
case IDLE : res = SetThreadPriority(th, THREAD_PRIORITY_LOWEST); break;
}
#endif
}
這是一個愚蠢的錯誤。我現在就放棄它。謝謝 – Lodle 2009-03-02 06:42:15