Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I change a programs priority? 1

Status
Not open for further replies.

2016nc

Programmer
Jun 3, 2002
31
US
I made a program to check the state of a key and writes a file based on the last time the key was pressed. I am doing to help me time events in a game. So both this program and the game will be running at the same time. The problem is this simple program strives to take up 100% of the processor time so this causes the game to run slowly. I am only checking the state of the key once every second. Is there a way to lower the programs priority or somehow tell it not to try so hard so the game won't suffer?
 
You shouldn't write to the file every second. Create a buffer to store those pressed keys and write them once the buffer is full. Writing to the file involves hardware interaction and therefore a considerable amount of processor time.

I am not sure what OS environment you plan your program to run but, if you are writing for Windows OS, there is no need to check the state of the keys every second. Windows will notify your program when user presses a key.
 
Changing the thread priority worked!!! I bumped my program down to idle and viola. Thanks guys. Here is a little more about my program to clear some things up if you are interested.

My program doesn't have focus, so windows doesn't send it a WM_KEYDOWN message or anything. So, I set up a WM_TIMER message for every second and GetAsyncKeyState() to check the state of the key I am interested in. The file has to be written every second because this is for a program to help time events to a resolution of one second. I don't know if this matters, but only 20 characters or so get written.

All that doesn't matter though, because if I take all the file writing leaving just the skeleton the program still races through eating up resources causing the game to run poorly.

here is the snippet of code that is at the heart of my program wanting my processor all to itself


/*********************************
// This snippet ganked from "prima tech's 'the zen of
// direct3d game programming' by Peter Walsh"
// I liked the book

// Start the message loop
while( TRUE )
{
// Check if a message is waiting for
//processing
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
// Check if the message is to quit
// the application
if( msg.message == WM_QUIT )
// Exit the message loop
break;

// Change the format of certain
// messages
TranslateMessage( &msg );
// Pass the message to WndProc()
//for processing
DispatchMessage( &msg );
}
}
***************************************************/

I originally had the code another way, which wouldn't eat up resources, however then the WM_TIMER events weren't handled for whatever reason. It looked like this.

/****************************************************
// This snippet also ganked from "prima tech's 'the zen of
// direct3d game programming' by Peter Walsh"
// I liked the book

// Start the message loop
while( GetMessage( &msg, NULL, 0, 0 ) )
{
// Modify key messages
TranslateMessage( &msg );
// Send the message to WndProc() the event
// handler
DispatchMessage( &msg );
}
*****************************************************/

Thanks again guys for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top