Archived

This topic is now archived and is closed to further replies.

ceze

10 times a second

Recommended Posts

I have this function, void anything(void). And I wan''t it to be called for instance ten times a second, or every second or whatever. I could do a loop and call timeGetTime() (or a similiar function) until 100ms have passed but that would be of waste of CPU-processing. So how do I do it? (I can imagine it''s pretty easy but hey, there was a time when you didn''t knew it neither)

Share this post


Link to post
Share on other sites
You call the SetTimer() function to setup the timer info. A WM_TIMER message is generated when the correct time has passed. Look at SetTimer() in the help docs for more detail.

[edited by - denethor on October 19, 2002 2:13:17 PM]

Share this post


Link to post
Share on other sites
One thing to be carefull of with this though is that the WM_TIMER message is a low priority message. You may not get exactly 10 times a second. As denethor said, read the docs. You can find details about this...

Clickity

Always remember, you''''re unique. Just like everyone else.

Share this post


Link to post
Share on other sites
hey! i said it was easy! =D

thanks

Greven: it doesn't matter in this case

but, btw: if one wanted it to be exactly 10 times a second, how would you do that? (just my curiosity) =)

[edited by - ceze on October 19, 2002 2:23:04 PM]

Share this post


Link to post
Share on other sites
Call a function constantly. Look at how much time has elapsed since last execution of your 10 per sec function. Exec your 10 per sec funtion accordingly (ie 2 times if it''s 200 ms)...

Extatica is coming soon!
Check it out on:
http://www.extatica.com02.com
Nexeruza Studios:
http://nexeruza.ionichost.com/home.html

Share this post


Link to post
Share on other sites
but that was exactly what I didn''t wan''t to do (if I understood you right).
I wanted the CPU to be able to do something diffrent while I was waiting for the 100ms to pass.

the SetTimer() can be called with a pointer to a function and then I can do whatever else I like (or nothing at all). When x ms have passed the function is called. Or if you rather wan''t an WM_TIMER message you can have that by not passing a function-pointer to SetTimer.

Share this post


Link to post
Share on other sites
you mean a loop like this
while(!done) {
while(timeGetTime()-lasttime < wait);
last_time=timeGetTime();
//do something
}

yes, but no. That loop will use the CPU 100%.

SetTimer does exactly that. And one don''t have to bother about it, just initialize it and kill it whenever you wan''t. And it will use the CPU just as much as needed.

Share this post


Link to post
Share on other sites
I guess something like this should do the trick:

while(!done)
{
Elapsed = (timeGetTime()- Timer);
IdleTime += Elapsed;
Timer = timeGetTime();

if( IdleTime >= 10seconds)
// do function and set IdleTime = 0
else
// do something else
}



[edited by - Mako on October 19, 2002 4:04:08 PM]

Share this post


Link to post
Share on other sites
Or you can "roll your own" timer thread for whatever reason:


  

// time critical thread function

DWORD WINAPI AniThreadProc( LPVOID lpParameter );

// globals

static HANDLE ghAniThread = NULL; // Handle of time critical thread

static DWORD gdwAniThreadID = 0; // ID of time critical thread

static UINT guAniTimer = 100; // ms to sleep


// mutex for time critical use

static CRITICAL_SECTION gcsAniThread;


int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
InitializeCriticalSection(&gcsAniThread);

// create the time critical thread but suspend it so that it doesn''t start processing yet

ghAniThread = CreateThread(NULL, 0, AniThreadProc, 0, CREATE_SUSPENDED, &gdwAniThreadID);

//

// do setup stuff here

//


// okay now to start the time critical thread

if( ghAniThread )
ResumeThread(ghAniThread);

//

// do normal processing stuff here

//


// get rid of time critical thread

if( ghAniThread ) {
UINT uAniWait = guAniTimer;
EnterCriticalSection(&gcsAniThread);
guAniTimer = 0;
LeaveCriticalSection(&gcsAniThread);
Sleep(uAniWait);
}

DeleteCriticalSection(&gcsAniThread);

return(0);
}


// time critical thread

static DWORD WINAPI AniThreadProc( LPVOID lpParameter )
{
SetThreadPriority(ghAniThread, THREAD_PRIORITY_TIME_CRITICAL);

while( guAniTimer ) {
Sleep(guAniTimer);
EnterCriticalSection(&gcsAniThread);
//

// do time critical stuff here

//

LeaveCriticalSection(&gcsAniThread);
}

return 0;
}

Share this post


Link to post
Share on other sites
ceze, just for the record, S1CA has the definitive answer... but why didn''t you look it up yourself? timeGetTime is in the same family of functions.

The signal-to-noise ratio here is quite low... I highly recommend referring to MSDN *before* you bring a question here.

Share this post


Link to post
Share on other sites