Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to set a countdown timer

Posted on 1998-07-07
6
Medium Priority
?
230 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
Industry Leaders: 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

610 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