#### Archived

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

## Recommended Posts

Architekt    122
Perhaps I''m confusing terminology..if so someone please correct me. Anyways, I''ve got a mouse update/polling function that I''d ideally like to be automatically called every certain number of times per second (roughly of course). I had used the Windows SDK CreateThread method to try and get that to work (I''m skipping over the non important stuff here), and it just gets called once and then exits. That makes sense to me, cause it''s just starting a new thread, which isn''t supposed to be called again after it finishes execution. But what I can''t figure out is how to get my update/polling function to be run in a separate thread AND be called repeatedly a certain number of times per second. Can someone explain or at least point me to the right MSDN page? Thanks.

##### Share on other sites
Anthracks    122
I don''t have time to go into detail now, but why not make the function your thread runs contain a simple loop?

  // pseudo codeint someFunction(){ while(gameIsRunning) { if(currenttime==nextTime) { pollInput(); nextTime=currenttime+100; } } return 0;}

Basically, just keep looping through as long as the game is still running. Get the time, and if it''s been a certain inverval (say 100 milliseconds) since the last time you polled, do it again, then set the next time to try to 100ms in the future.

Make sense? Someone will probably give a better answer later on anyway heh.

Anthracks

##### Share on other sites
Oluseyi    2103
The function you pass to CreateThread should sit in a loop and poll the mouse. To terminate it, you can send it a message (SendThreadMessage) or use a Mutex.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

##### Share on other sites
Shannon Barber    1681
Dont forget to Sleep(10) in that thread or you''ll be disapointed

##### Share on other sites
Fruny    1658
Don''t forget that if you use CreateThread you cannot use the C runtime library. Use beginthread instead.

##### Share on other sites
Infinisearch    2967
Check your SDK both WIN32 and DX (if u are using it) for timer functions. Once you setup the timer, it sends your app messages handled by your window message procedure(WM_TIMER). From here you could either:
1.Don't use threads at all, Poll using a procedure, it being the WM_TIMER message handler. (No extra overhead since u have to deal with the message loop.)