Solved

How to set a countdown timer

Posted on 1998-07-07
6
206 Views
Last Modified: 2010-05-18
Hi,

I'd need to know how to set a timer in C/C++, so that at the end of the given time, it executes what i want it to do.
But in the meantime of that happening, i would like the program to execute other tasks.
This would be like a separate process.

thanx for any suggestions and help
0
Comment
Question by:mvu
  • 3
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 200 total points
ID: 1167301
Here is a suggestion.  When you want to start the timer, use the function SetTimer like so:
SetTimer (HandleOfWindow, Idenifier, LengthOfTime, AddressOfTimerHandlingFunction);

Here is what this all means:
HandleOfWindow - This is the handle of the window that you want this time to be elapsing in.

Identifier - This is just a number (say 1) that identifies the timer.  When you want to stop the timer from going (probably once the desired amount of time has gone by, you would use:
KillTimer (Identifier);

LengthOfTime - This is the amount of time that will pass, in milliseconds, until the function specified as the last parameter is executed.

AddressOfTimerHandlingFunction - This is the address of a function that will be called once the desired amount of time has gone by.

Within the function that you give it, call KillTimer (Identifier) if you don't want the timer to keep on going (if you only want it to be called once.  If you want it to be called over and over, just call KillTimer (Identifier) at the end of the program.

Hope this helps.  If you have any questions, ask.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1167302
Oh, one last thing I need to add, the function that you want the timer function to call must be in this form:

VOID CALLBACK TimerProc( HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message

UINT idEvent, // timer identifier
 
DWORD dwTime // current system time
);

Try looking up TimerProc in the help file for more information on this function.
0
 

Author Comment

by:mvu
ID: 1167303
Thanx for the response.  How can I get to be in Seconds or hours, instead of milliseconds?

so the way it runs would be something like this:

SetTimer(NULL,1,timeout in msecs,"name of function i like to run at end of timeout");


0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mvu
ID: 1167304
I did not see your second comment.  I am not sure i understand, the function i need to run must be in the TimerProc form....

Can you give an example?
0
 

Author Comment

by:mvu
ID: 1167305
Thanx! I am all set for what I needed to do, using timerproc and settimer.  
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1167306
Glad I could help :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question