![]() |
|
#1
|
|||
|
|||
how to create a timer in c++Hi all,
how can i create a timer in my application "i am writing c++" that when the timer timeout "lets say after 5 minutes" it will do something "close the transmission channel"? thanks alot. |
|
#2
|
||||
|
||||
Re: how to create a timer in c++Do you want the timer to run in the background or pause the entire program? What type of program are you writing? UNIX, WIN32?
This is an example of a blocking wait function: CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: how to create a timer in c++Quote:
I'm not sure that this is going to have the desired effect. CPP / C++ / C Code:
Also, there are a lot of situations where it is "bad" to spin, especially in battery powered devices where spinning software consumes limited battery reserves. In such cases, hardware timers and any necessary device driver interfaces are probably preferred. I'd probably recommend checking out getitimer for what appears to be the goal and let the signal handling facility provide the necessary "interrupt." An alternative "spin" is to use C timevals and appropriate functions, as they usually give adequate resolution, are usually portable and usually efficiently implemented...and, importantly, usually give the intended results of actually waiting the desired length of time. :davis: |
|
#4
|
||||
|
||||
Re: how to create a timer in c++I usually use Win32 timers. When the timer is completed the window sends a WM_TIMER message and you can handle it then.
That code does not end in .2 for me though, im not sure if your libraries declare time_t differantly. I personally don't like that aproach anyway because it eats up system resources on long waits. |
|
#5
|
|||
|
|||
Re: how to create a timer in c++Quote:
WM_TIMER messages handled in the main window procedure may not be dealt with efficiently, especially if a lot of work is being done in say a WM_PAINT handler. Using Win32 SetTimer with a named TimerProc will usually provide better response. Quote:
It is typedef'd as a long under GNU/Linux, which is the same as for Win32. Quote:
What else does the term "spin" mean? FWIW: "eats up system resources" is fairly non-specific. It consumes processor cycles. It isn't as if the memory usage is increasing as a factor of the spin code. :davis: |
|
#6
|
|||
|
|||
Re: how to create a timer in c++hey thanks alot for all these replies
Quote:
|
|
#7
|
|||
|
|||
Re: how to create a timer in c++Quote:
Take a look at using Qt. Qt has a QTimer class that offers a platform-agnostic approach to implementing a suitable timer. :davis: |
|
#8
|
|||
|
|||
Re: how to create a timer in c++thanks davis, but i dont want to use external libraries like QT but i can use OS specific timer functions and use macros like this
CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
Re: how to create a timer in c++Quote:
Thanks A LOT again...a lot is two words. Also, if you're too lazy to spell out "you," why should I be "unlazy" enough to help "u?" You don't need to USE Qt, but if you LOOK at how they do their QTimer class implementation, perhaps you'll have some perfect example source of how you might consider doing your own. If you've got a recent Linux machine, chances are it is already installed and the sources are there for you to consider. If not, just insert your sources disk(s) and install the Qt sources or extract the archive and take a LOOK. :davis: |
|
#10
|
||||
|
||||
Re: how to create a timer in c++CPP / C++ / C Code:
To create a timer you can create a background thread and use the sleep function. Windows and Linux use differant thread libraries though so you will want to read up on both of them (Win32 and POSIX threads). The sleep function is also slightly differant in Windows, it takes milliseconds as an argument instead of whole seconds. LINUX: Code:
WINDOWS: Code:
|
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Steps to create and compile c program in SUSE | batrsau | Computer Software Forum - Linux | 7 | 03-Jun-2007 19:49 |
| How to create Trialware (30-days or run counting)? | stormlab | C++ Forum | 4 | 13-Oct-2005 06:16 |
| How do i create a mfc timer | teck hui | MS Visual C++ / MFC Forum | 2 | 25-Nov-2004 20:59 |
| Can't seem to create db | Tigress7 | MySQL / PHP Forum | 3 | 19-Aug-2003 09:19 |
| [class] Timer | JdS | PHP Code Library | 2 | 29-Apr-2003 12:20 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The