Solved

How to set a countdown timer

Posted on 1998-07-07
6
219 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 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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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