Calling any multi-thread wizards. Here is a copy of the relevant parts of my main function...

CWinApp theApp; //the application's handle? not sure...

using namespace std;

UINT callProcessThread(LPVOID minutesSpent); //worker thread function, process output
UINT initializeProgram(LPVOID flag); //worker thread function, load MCR
void inputCalculateOutput(void); // function that sorts data and calls callProcessThread

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{   HANDLE threadHand = GetCurrentThread();
	CWinThread *startThread;
    int nRetCode = 0, priority;

	SetPriorityClass(threadHand, HIGH_PRIORITY_CLASS);
	SetThreadPriority(threadHand, THREAD_PRIORITY_NORMAL);

    //initialize the MCR
    startThread = AfxBeginThread(initializeProgram, 
		                         &nRetCode,
		                         THREAD_PRIORITY_HIGHEST); 

	std::cout << "Initializing, please wait.\n";

	// initialize MFC and print and error on failure
    while(INFINITE)
	{if(WaitForSingleObject(*startThread, INFINITE)
	                              == WAIT_OBJECT_0)
	 {//select a priority class for the program...
	  std::cout << "Select a priority class for the program\n"
          << "1) Idle\n2) Below Normal\n3) Normal\n4) Above Normal\n"
          << "5) High\n6) Highest\n\n >> ";
       
	  if(std::cin >> priority)
	  {
	   switch(priority)
	   {
		case 1:
         SetPriorityClass(threadHand,IDLE_PRIORITY_CLASS);
		 SetThreadPriority(threadHand,THREAD_PRIORITY_BELOW_NORMAL);
			break;
		case 2:
		 SetPriorityClass(threadHand,BELOW_NORMAL_PRIORITY_CLASS);
		 SetThreadPriority(threadHand,THREAD_PRIORITY_BELOW_NORMAL);
			break;
		case 3:
		 SetPriorityClass(threadHand,NORMAL_PRIORITY_CLASS);
		 SetThreadPriority(threadHand,THREAD_PRIORITY_NORMAL);
			break;
		case 4:
		 SetPriorityClass(threadHand,ABOVE_NORMAL_PRIORITY_CLASS);
		 SetThreadPriority(threadHand,THREAD_PRIORITY_ABOVE_NORMAL);
			break;
		case 5:
		 SetPriorityClass(threadHand,HIGH_PRIORITY_CLASS);
		 SetThreadPriority(threadHand,THREAD_PRIORITY_HIGHEST);
			break;
		case 6:
		 SetPriorityClass(threadHand,REALTIME_PRIORITY_CLASS);
		 SetThreadPriority(threadHand,THREAD_PRIORITY_TIME_CRITICAL);
			break;
	    } 
	    inputCalculateOutput(); //take input, calculate output...
	    break;
	  }
       else
	   {cin.ignore(100000,'\n');
		cin.clear();
		break;
	   }
     }
   }

	 return nRetCode;
}

As you can see, I am trying to allow the user to set the priority class of the program through the command line. No matter what selection is made, when I check the priorities through task manager application.exe is always 'normal' priority. Additionally I cannot make one instance of application.exe run with priority over another instance of application.exe. Should I switch threadHand for theApp ? or am I doing something else wrong? Does anyone know of a sure fire way for me to set the priority class of my program? (For example explorer.exe defaults to 'High' priority according to task manager... I want to be able to do that!) Is there a utility to view threads and how they tick on and off of the processor?

Recommended Answers

All 2 Replies

SetPriorityClass takes the handle of the process, not the thread's handle. So you need to have

HANDLE hProcess = GetCurrentProcess();

Is there a utility to view threads and how they tick on and off of the processor?

You might use
Process Explorer

ah! I didn't even notice that! That certainly would make a difference I imagine...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.