Pregunta

Estoy intentando cambiar la prioridad del hilo en impulso pero no estoy teniendo suerte. Estoy obteniendo un error de manejo incorrecto (tipo 6) de la función GetLastError. ¿Aunque Native_handle () devolví el identificador del hilo?

¿Alguien sabe cómo hacer esto?

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
}

editar: Código final:

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
}
¿Fue útil?

Solución

Use la función SetThreadPriority para establecer la prioridad del hilo. SetPriorityClass se utiliza para establecer la prioridad del proceso. También tiene que cambiar los valores de prioridad, consulte la documentación para SetThreadPriority para más detalles.

Otros consejos

La función SetPriorityClass toma como primer parámetro un HANDLE, le está pasando un puntero a un HANDLE. Cambiarlo a:

res = SetPriorityClass(*th, REALTIME_PRIORITY_CLASS);

o algo equivalente. El kernel puede decir que el valor del puntero que pasó no es realmente un identificador de subproceso válido porque supongo que mantiene una lista interna de los asideros de subproceso asignados actualmente. El puntero obviamente no está en esa lista. El compilador realmente no puede imponer una mejor seguridad de tipos, ya que HANDLE es un tipo opaco, solo debes tener mucho cuidado con lo que pasas.

Por cierto, el otro comentarista Dani es correcto, SetPriorityClass no se usa para establecer la prioridad de un subproceso, de todos modos quiere usar SetThreadPriority . Pero entonces mi consejo se mantendría, debe pasar una MANIJA, no un puntero a tal.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top