|
-
May 19th, 2009, 08:42 PM
#1
Implementing timers
Hi,
I have a program that currently does reads/writes to a storage device indefinitely, until the user closes the program. I would like to have a timer that stops the read/write loop after x seconds. I'm wondering what's the best/easiest way to implement this? I'm not familiar with multi-threading programming but it seems like this could be 1 way?
Thanks.
-
May 19th, 2009, 11:46 PM
#2
Re: Implementing timers
Look at MSDN: Using Timers.
Cheers
-
May 25th, 2009, 01:14 AM
#3
Re: Implementing timers
Thanks. My program is a console program though, so the main loop is done in main(). I'm not sure what hwnd should be(NULL?), nor whether to set it up with a callback function or not.
Also when I tried to use SetTimer() with some timer identifier ITD_TIMER1, it complains that it's unidentified. Do I have to declare it somewhere? The MSDN examples weren't very helpful as they didn't show how or where to do all this.
Thanks again.
Edit:
I found this example code here for console programs, and I've tried it and it works as it claims. However, it's a while loop that simply keeps printing messages at a set time interval. Since it's a while loop, I can't do anything other than wait to print out the messages. What I want instead is for the timer to interrupt what I'm doing(a for loop that's reading/writing to a storage device) and then break out of the read/write loop. I can't figure out how to do this with timers?
Last edited by galapogos; May 25th, 2009 at 02:01 AM.
-
May 25th, 2009, 07:35 AM
#4
Re: Implementing timers
You should be able to use SetTimer and set it up using a callback function. When your callback function is called you can set a boolean variable to TRUE and check that value of that variable in your loop. When you detect it is true, abort your loop.
-
May 25th, 2009, 08:03 AM
#5
Re: Implementing timers
You mean something like this?
Code:
bool flag = false;
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) {
flag = true;
}
int main(int argc, char *argv[], char *envp[]) {
UINT TimerId = SetTimer(NULL, 0, 500, &TimerProc);
if (!TimerId)
cout << "SetTimer error" << endl;
for (;;)
{
// Read/write here
// ...
if (flag == true)
break;
}
KillTimer(NULL, TimerId);
return 0;
}
Do I even need GetMessage()/DispatchMessage()?
-
May 25th, 2009, 09:35 AM
#6
Re: Implementing timers
It turns out that even when using SetTimer with the callback parameter, you still need a message pump. It's also not recommended to use SetTimer in console application is seems.
Try to use a multimedia timer instead, timeSetEvent.
-
May 25th, 2009, 09:40 AM
#7
Re: Implementing timers
Thanks. I'm guessing I use timeSetEvent in the same way, i.e. the callback function sets a flag that the loop checks, and breaks out of if it's true?
Seems like timeSetEvent is obsolete and superceded by CreateTimerQueueTimer too.
-
May 25th, 2009, 12:26 PM
#8
Re: Implementing timers
 Originally Posted by galapogos
Thanks. I'm guessing I use timeSetEvent in the same way, i.e. the callback function sets a flag that the loop checks, and breaks out of if it's true?
Yes.
-
May 25th, 2009, 10:02 PM
#9
Re: Implementing timers
Sorry, but is there any example code for this? I'm having trouble declaring my callback function. Not sure of the syntax.
Thanks.
-
May 27th, 2009, 05:22 AM
#10
Re: Implementing timers
The signature of your callback should be:
Code:
void (CALLBACK)(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
Coming from http://msdn.microsoft.com/en-us/libr...23(VS.85).aspx
-
May 27th, 2009, 07:27 AM
#11
Re: Implementing timers
Thanks. I ended up using CreateTimerQueueTimer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|