GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Feb-2006, 08:18
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

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  
Old 21-Feb-2006, 08:52
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

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:
#include <time.h>

void Wait(int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}
  #3  
Old 21-Feb-2006, 09:54
davis
 
Posts: n/a

Re: how to create a timer in c++


Quote:
Originally Posted by Chris.Dev
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:
#include <time.h>

void Wait(int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}

I'm not sure that this is going to have the desired effect.

CPP / C++ / C Code:

#include <stdio.h>
#include <time.h>

void Wait(int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}

int main( void )
{
    printf( "Waiting..." );
    Wait( 400 );
    printf( "Done Waiting!\n" );

    return 0;
}

// output:

time ./blkwait
Waiting...Done Waiting!

real    0m0.194s
user    0m0.018s
sys     0m0.033s

...completed in under .2 seconds!

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  
Old 21-Feb-2006, 10:03
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

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  
Old 21-Feb-2006, 10:53
davis
 
Posts: n/a

Re: how to create a timer in c++


Quote:
Originally Posted by Chris.Dev
I usually use Win32 timers. When the timer is completed the window sends a WM_TIMER message and you can handle it then.

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:
Originally Posted by Chris.Dev
That code does not end in .2 for me though, im not sure if your libraries declare time_t differantly.

It is typedef'd as a long under GNU/Linux, which is the same as for Win32.

Quote:
Originally Posted by Chris.Dev
I personally don't like that aproach anyway because it eats up system resources on long waits.

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  
Old 21-Feb-2006, 12:25
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Re: how to create a timer in c++


hey thanks alot for all these replies
Quote:
Do you want the timer to run in the background or pause the entire program? What type of program are you writing? UNIX, WIN32?
yes the timer will run in the background but dont pause the program, and for the development enviroment well both GNU/LINUX and Win32.
  #7  
Old 21-Feb-2006, 13:11
davis
 
Posts: n/a

Re: how to create a timer in c++


Quote:
Originally Posted by Radi0ShacK
hey thanks a lot for all these replies

yes the timer will run in the background but dont pause the program, and for the development enviroment well both GNU/LINUX and Win32.

Take a look at using Qt. Qt has a QTimer class that offers a platform-agnostic approach to implementing a suitable timer.


:davis:
  #8  
Old 21-Feb-2006, 23:38
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

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:
#if defined WIN32
//start windows timer
#endif
#if defined __LINUX__
//start linux timer
#endif
so can u tell me what functions to use or what i read in order to know what to do? thanks alot again.
  #9  
Old 22-Feb-2006, 07:47
davis
 
Posts: n/a

Re: how to create a timer in c++


Quote:
Originally Posted by Radi0ShacK
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:
#if defined WIN32
//start windows timer
#endif
#if defined __LINUX__
//start linux timer
#endif
so can u tell me what functions to use or what i read in order to know what to do? thanks alot again.

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  
Old 22-Feb-2006, 08:48
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: how to create a timer in c++


CPP / C++ / C Code:
#ifdef _WIN32
# include <windows.h>
# define sleep(x) Sleep(x * 1000)
#else
# include <unistd.h>
#endif

int main(int argc, char *argv[]) {
    sleep(10);
    return 0;
}

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:
#include <unistd.h> unsigned int sleep( unsigned int seconds )

WINDOWS:
Code:
#include <windows.h> VOID Sleep( DWORD dwMilliseconds )
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.