Timer

I was wondering if anyone knew how to make a timer? One where the user put in a certain time, and when the counter reaches zero/reached that time, the program would do something that would cause the bottom of the screen to appear and flash, indicating something has happened. I have no idea how to do this, so I have no base code to edit or add on to.
Console or GUI Application?
GUI, but I found some code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream.h>                         
#include <stdio.h>
#include <time.h>
#include <windows.h>

void wait ( int seconds )
{
	clock_t endwait;
	endwait = clock () + seconds * CLOCKS_PER_SEC ;
	while (clock() < endwait) {}
}

int main ()
{
	int n;
	printf ("Starting countdown...\n");
	for (n=1200; n>0; n--)
	{
		printf ("%d\n",n);
		wait (1);
	}
	printf ("FIRE!!!\n");
	printf ("Countdown over\n");

	cin.get();
	return 0;
}


But I'm not particularly sure about what's in the "void wait(...)" part and the "printf("%d\n",n) part. Could you please explain those to me?
Last edited on
Bump
For (Windows GUI) function - you can use the Windows Timer.
For the flasing part you can possibly use the FlashWindow function:

Information on using the Timer here:
http://msdn.microsoft.com/en-us/library/ms644906.aspx

Information on flasing the window here:
http://msdn.microsoft.com/en-us/library/ms679346(VS.85).aspx
Possibly something like -
1
2
3
4
5
6
7
8
9
10
11
void wait(double val)
{
	int i;

	for (i = 0; i <= val; i++)
	{
		continue;
	}
}

wait(4000000); //etc 
That isn't a good timer as it depends on the speed of the computer which may vary during the execution of the program...
I know it's not a good one, just throwing out another option that he could possibly use - and haha trust me i've seen myself with the speed of a computer when you use anything with loops on a laptop - if you change the battery setting or the power goes out while your running something. Damn big decrease in speed lol
If your developing a GUI application there should be a timer item that will have a callback parameter. Creating the timer and it'll run in it's own thread not tieing up the CPU until it's time is up then call the callback.

If your going to have a blocking timer (e.g stops everything til it's done). Then it's best to use the Sleep() method or a message loop. A message loop would be more beneficial because it'd allow you to process other events (e.g GUI Updates).

The best method if writing your own. Have a class that creates it's own thread. In this thread use the Sleep() call and return after the amount of time. Threading will give you a non-blocking timer.

However, I am 95% sure that Visual Studio has timer objects you can use.
Sleep()! Thats the one I use to use a long time ago. I totally forgot about it hah. Thank you Zaita!
Umm, okay... I'll settle with a simple console app...

I just want help on a timer that'll play a sound when it's done and won't take up any CPU. I have no idea what half of you said means, Zaita, so i guess I'll just stop asking and settle on not coding...
Maybe you should like use SDL or something - read up on it's documentation and see the audio and timer files the SDL has to offer?
I've looked at SDL; for some reason it won't compile with some weird errors.
Is that weird error something to do with not being able to find main
or winmain functions?
No, it says a few things along the lines of "so and so doesn't exist" and "so and so wasn't declared in this scope." I tried emailing CrazyFoo, but he's at college, and yeah.
Hi,
I didn't understand your sentence:
a timer that'll play a sound when it's done ...

If you need to play a sound when your code is finished, you don't need a timer...

But (according to your initial post) if your goal is to simulate a countdown behaviour, use the good old Windows timer (see guestgulkan's post):

SetTimer (hwnd, 1, TimerInterval, (TIMERPROC)TimerProc);

which will repeatedly call your

void CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime); // define a function somewhere with this prototype

after every "TimerInterval" miliseconds until you finally call

KillTimer (hwnd, 1);

If you need to perform a certain set of commands only once, call the KillTimer() procedure inside your TimerProc() to disable the timer. Of course, the flashing or whatever action you need is coded in the TimerProc (or called from within).
Note: To have more timers in your app, change the second parameter (I chose 1) which has to be the same in evry pair of SetTimer() and KillTimer().

Cheers.
@Mythios: no worries :) I do a fair amount of multi-threaded development and it's something you actually use quite frequently. Especially with socket development.

@Console: Look at poloniny's answer. That's the windows timer I referred to and the simplest way to achieve your result.
Excellent :D
Topic archived. No new replies allowed.