Solved

How to set a countdown timer

Posted on 1998-07-07
6
179 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now