Sign in to follow this  
mrmrcoleman

Error code problems when changing thread priority class.

Recommended Posts

I have this code in my app to change the priority class of a worker thread which I create.
if(!(SetPriorityClass(Handle1, IDLE_PRIORITY_CLASS)))
	MessageBox(0, "Could not lower the priority class for thread 1", "Test", 0);

DWORD error = GetLastError();

The SetPriorityClass is failing, i.e. returning 0, however when I check the error code with GetLastError() it gives me a 0, which apparently means that operation completed successfully? Am I missing something obvious? Kind regards, Mark Coleman.

Share this post


Link to post
Share on other sites
try:

DWORD error;

if(!(SetPriorityClass(Handle1, IDLE_PRIORITY_CLASS)))
{
error = GetLastError();
MessageBox(0, "Could not lower the priority class for thread 1", "Test", 0);
}





edit: darn.. beaten to it :P

Share this post


Link to post
Share on other sites
Turns out the handle is created fine so it must be something to do with this...



From MSDN:

BOOL SetPriorityClass(
HANDLE hProcess,
DWORD dwPriorityClass
);

Parameters
hProcess
[in] Handle to the process.
****The handle must have the PROCESS_SET_INFORMATION access right. For more information, see Process Security and Access Rights.**** <--- THIS

dwPriorityClass
[in] Priority class for the process. This parameter can be one of the following values.



But I can't for the life of me figure out how to set this access right...

Doe's anybody have any idea? The MSDN documentation seems to be circular on the subject.

Mark Coleman

Share this post


Link to post
Share on other sites
SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);

Should work, i think Process Security and Access Rights has something to do with the logged on user, could be totally wrong though.

Share this post


Link to post
Share on other sites
Quote:
Original post by mrmrcoleman
I have this code in my app to change the priority class of a worker thread which I create.


Just to be sure: Aren't you using the wrong function?

The SetPriorityClass function sets the priority class for the specified process.

The SetThreadPriority function sets the priority value for the specified thread.

If you set the current process to idle I think you may get into trouble.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this