Error code problems when changing thread priority class.

Started by
8 comments, last by dalleboy 18 years, 10 months ago
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.
Advertisement
Maybe MessageBox is setting the ERROR_SUCCESS?
Thats a good point... Back in a mo..

Mark
Turns out the handle was invalid.

Time for bed.

Mark
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
- To learn, we share... Give some to take some -
...you passed the thread ID to the function, didn't you?
Which function?
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
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.
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.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement