当前位置:  开发笔记 > 编程语言 > 正文

在Windows中更改提升线程优先级

如何解决《在Windows中更改提升线程优先级》经验,为你挑选了1个好方法。

我试图改变提升中的线程优先级,但我没有运气.我从GetLastError函数得到一个错误的句柄错误(类型6).我虽然native_handle()返回了线程的句柄?

有人知道怎么做吗?

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
}

Dani van der.. 18

使用SetThreadPriority函数设置线程优先级.SetPriorityClass用于设置进程的优先级.您还必须更改优先级值,有关详细信息,请参阅SetThreadPriority的文档.



1> Dani van der..:

使用SetThreadPriority函数设置线程优先级.SetPriorityClass用于设置进程的优先级.您还必须更改优先级值,有关详细信息,请参阅SetThreadPriority的文档.

推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有