?
Solved

How to set a countdown timer

Posted on 1998-07-07
6
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

770 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