How to set the Thread priority AfxBeginThread

Hi experts,
I ran into problems, using this:
DWORD priority = TranslatePriority("Low");
AfxBeginThread(WorkThread,this,int(priority),0);
For some strange reason this crashes.
Any ideas?

Priority is generated from a CComboBox like this:
DWORD CAutoPeaksDlg::TranslatePriority(string str){
      DWORD ret = IDLE_PRIORITY_CLASS;
      if(str == "Highest")
            ret = REALTIME_PRIORITY_CLASS;
      if(str == "High")
            ret = HIGH_PRIORITY_CLASS;
      if(str == "Above Normal")
            ret = ABOVE_NORMAL_PRIORITY_CLASS;
      if(str == "Normal")
            ret = NORMAL_PRIORITY_CLASS;
      if(str == "Below Normal")
            ret = BELOW_NORMAL_PRIORITY_CLASS;
      if(str == "Low")
            ret = IDLE_PRIORITY_CLASS;
      return(ret);
}
Thanks,
Jens
LVL 5
allmerAsked:
Who is Participating?
 
rcarlanCommented:
First of all, to answer your question, priority classes are for processes not for threads.

Use

THREAD_PRIORITY_IDLE
THREAD_PRIORITY_LOWEST
THREAD_PRIORITY_BELOW_NORMAL
THREAD_PRIORITY_NORMAL
THREAD_PRIORITY_ABOVE_NORMAL
THREAD_PRIORITY_HIGHEST
THREAD_PRIORITY_TIME_CRITICAL

for threads.


Secondly, just as a general comment, you may want to either replace the ifs with else-ifs or return as soon as you have a match. Your implementation of TranslatePriority is rather inefficient because it compares against all strings even after finding a match.

Radu

0
 
allmerAuthor Commented:
Thanks Radu,
I got mixed up there.
And yes you are right about the implementation, i'll switch it.
Best,
Jens
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.