Search code examples
multithreadingwinapiprocesswindows-servicesthread-priority

Setting a thread priority in a service has no effect


Is there some additional configuration needed before I can set thread priorities in a Windows service?

In my service, I have a few threads that each call the CreateProcess() function to launch an external application. I would like to adjust thread (or process) priorities to normal or lower, depending on some other factors.

The problem is that SetThreadPriority() function fails with an error 6 (invalid handle?). I'm passing in a handle obtained from PROCESS_INFORMATION::hThread (after calling the CreateProcess() of course), so I think that the handle should be valid.

I've also tried setting the priority on the processes using the SetPriorityClass() function, which also fails.

The service is logged on as a local user.


Solution

  • Maybe you don't have the correct access rights? MSDN on SetThreadPriority says:

    hThread [in] A handle to the thread whose priority value is to be set.

    The handle must have the THREAD_SET_INFORMATION or THREAD_SET_LIMITED_INFORMATION access right. For more information, see Thread Security and Access Rights.

    Windows Server 2003 and Windows XP/2000: The handle must have the THREAD_SET_INFORMATION access right.