Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Timer

Reply
Thread Tools

Timer

 
 
Gaijinco
Guest
Posts: n/a
 
      04-26-2006
How can I write a program to be executed for a given ammount of time.

I mean I want to write a program that once started it doesn't do
anything for like 2 minutes and then exits.

It is possible?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-26-2006
Gaijinco wrote:
> How can I write a program to be executed for a given ammount of time.
>
> I mean I want to write a program that once started it doesn't do
> anything for like 2 minutes and then exits.
>
> It is possible?


It is possible if it's possible in the OS you're using. You need to see
what "sleep" functionality your OS provides. Ask in the newsgroup
dedicated to your OS.

In C or C++ "doesn't do anything" is impossible. The only "delay" you
can implement using standard means of either language is in line with

/* take time reading, use 'time' */
for (;
{
/* take another time reading */
/* check if time isn't over, use 'difftime' */
/* if it's over, break; */
}

but it definitely does not qualify as "doing nothing".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
invisal@gmail.com
Guest
Posts: n/a
 
      04-26-2006

Gaijinco wrote:
> How can I write a program to be executed for a given ammount of time.
>
> I mean I want to write a program that once started it doesn't do
> anything for like 2 minutes and then exits.
>
> It is possible?


Hi I am new here. If you are programming with window platform. you can
used Sleep function from the windows.h heander. Which look something
like this:

#include <iostream>
#include <windows.h>

int main()
{
std::cout << "Timer started! \n";
Sleep(120000);
std:;cout << "2 Minutes is over! \n"
}

Sleep will pause the whole program. Sleep(1000) mean pause for 1
second, Sleep(1) mean pause for 1milli-second.

I hope this information might help you.

From
Visal .In

 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      04-26-2006

Gaijinco wrote:
> How can I write a program to be executed for a given ammount of time.
>
> I mean I want to write a program that once started it doesn't do
> anything for like 2 minutes and then exits.
>
> It is possible?


Maybe. Depends on your exact requirements. Standard C has <time.h>
header which shoudl contain stuff you'd need. Look it up, but beware:
not many things are guaranteed. The stuff you'd likely need is:

CLOCKS_PER_SEC

and

clock_t clock(void);

which returns the number of clock cycles since the start of your
program. Divide by CLOCKS_PER_SEC to get to the seconds. The function
returns `(clock_t)(-1)` if the information is not available or can't be
represented.

Of course, if you don't care about portability, you can always use
whatever's available and specific to your platform. You should ask
about these in an appropriate group (this one not being appropriate for
platform specific questions).

 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      04-26-2006

(E-Mail Removed) wrote:
> Gaijinco wrote:
> > How can I write a program to be executed for a given ammount of time.
> >
> > I mean I want to write a program that once started it doesn't do
> > anything for like 2 minutes and then exits.
> >
> > It is possible?

>
> Hi I am new here. If you are programming with window platform. you can
> used Sleep function from the windows.h heander. Which look something
> like this:
>
> #include <iostream>


>From this point on, it belongs to comp.lang.c++ (although I guess

platform specific stuff is off-topic there as well).

Followups-to set...

> #include <windows.h>
>
> int main()
> {
> std::cout << "Timer started! \n";
> Sleep(120000);
> std:;cout << "2 Minutes is over! \n"
> }
>
> Sleep will pause the whole program. Sleep(1000) mean pause for 1
> second, Sleep(1) mean pause for 1milli-second.
>
> I hope this information might help you.
>
> From
> Visal .In


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-26-2006
Gaijinco wrote:
>
> How can I write a program to be executed for a given ammount of time.
>
> I mean I want to write a program that once started it doesn't do
> anything for like 2 minutes and then exits.
>
> It is possible?


Invoke the 'as if' rule from the standard. Do nothing. Stare at
the terminal for two minutes, then repeat doing nothing. QED.
Very economical and environmentally friendly.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-26-2006
"Vladimir Oka" <(E-Mail Removed)> writes:
> Gaijinco wrote:
>> How can I write a program to be executed for a given ammount of time.
>>
>> I mean I want to write a program that once started it doesn't do
>> anything for like 2 minutes and then exits.
>>
>> It is possible?

>
> Maybe. Depends on your exact requirements. Standard C has <time.h>
> header which shoudl contain stuff you'd need. Look it up, but beware:
> not many things are guaranteed. The stuff you'd likely need is:
>
> CLOCKS_PER_SEC
>
> and
>
> clock_t clock(void);
>
> which returns the number of clock cycles since the start of your
> program. Divide by CLOCKS_PER_SEC to get to the seconds. The function
> returns `(clock_t)(-1)` if the information is not available or can't be
> represented.


<time.h> doesn't provide a function that sleeps for a specified amount
of time. You could write a busy loop that runs until the current time
reaches a specified value, but that's a really bad idea on a
multi-processing system; while it's looping, your program will consume
CPU time at the expense of other processes on the system.

The clock() function returns an indication of the amount of CPU time
your program has consumed; it doesn't indicate real time.

> Of course, if you don't care about portability, you can always use
> whatever's available and specific to your platform. You should ask
> about these in an appropriate group (this one not being appropriate for
> platform specific questions).


Sleeping for a specified number of seconds is one of those things that
can be done much better using non-portable code. Most systems will
provide something like a sleep() function.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
siska
Guest
Posts: n/a
 
      04-26-2006

Keith Thompson wrote:
> "Vladimir Oka" <(E-Mail Removed)> writes:
> > Gaijinco wrote:
> >> How can I write a program to be executed for a given ammount of time.
> >>
> >> I mean I want to write a program that once started it doesn't do
> >> anything for like 2 minutes and then exits.
> >>
> >> It is possible?

> >
> > Maybe. Depends on your exact requirements. Standard C has <time.h>
> > header which shoudl contain stuff you'd need. Look it up, but beware:
> > not many things are guaranteed. The stuff you'd likely need is:
> >
> > CLOCKS_PER_SEC
> >
> > and
> >
> > clock_t clock(void);
> >
> > which returns the number of clock cycles since the start of your
> > program. Divide by CLOCKS_PER_SEC to get to the seconds. The function
> > returns `(clock_t)(-1)` if the information is not available or can't be
> > represented.

>
> <time.h> doesn't provide a function that sleeps for a specified amount
> of time. You could write a busy loop that runs until the current time
> reaches a specified value, but that's a really bad idea on a
> multi-processing system; while it's looping, your program will consume
> CPU time at the expense of other processes on the system.
>
> The clock() function returns an indication of the amount of CPU time
> your program has consumed; it doesn't indicate real time.
>
> > Of course, if you don't care about portability, you can always use
> > whatever's available and specific to your platform. You should ask
> > about these in an appropriate group (this one not being appropriate for
> > platform specific questions).

>
> Sleeping for a specified number of seconds is one of those things that
> can be done much better using non-portable code. Most systems will
> provide something like a sleep() function.
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.


I use this sometimes when writing short programs for
AIX/HPUX/Linux/Solaris and Windows, I define "WIN32" when compiling on
Windows (cl ... /D"WIN32 ..."):

***CODE EXAMPLE***

/* file to include - in whatever file it is needed */
#ifdef WIN32
#include <windows.h>
#else /* WIN32 */
#include <unistd.h>
#endif /* WIN32 */

....

/* define the function to use */
#ifdef WIN32
#define SLEEP_TIME 5000
#define SLEEP_FUNC Sleep
#else /* WIN32 */
#define SLEEP_TIME 5
#define SLEEP_FUNC sleep
#endif /* WIN32 */

....

/* someone deep in the code the function is used */
if( wait_for_something == true )
{
SLEEP_FUNC ( SLEEP_TIME );
}

***CODE EXAMPLE***

Of course this may not be the safest or best way to do it but it works
for simple programs.

Stephen W. Vickers

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-26-2006
"siska" <(E-Mail Removed)> writes:
> Keith Thompson wrote:

[...]
>> Sleeping for a specified number of seconds is one of those things that
>> can be done much better using non-portable code. Most systems will
>> provide something like a sleep() function.

[...]

Thank you for quoting context in spite of the Google Groups interface.

But it's seldom necessary to quote the entire article to which you're
replying. Please trim anything that's not relevant to your reply. In
particular, don't quote signatures unless you're commenting on them.

> I use this sometimes when writing short programs for
> AIX/HPUX/Linux/Solaris and Windows, I define "WIN32" when compiling on
> Windows (cl ... /D"WIN32 ..."):
>
> ***CODE EXAMPLE***
>
> /* file to include - in whatever file it is needed */
> #ifdef WIN32
> #include <windows.h>
> #else /* WIN32 */
> #include <unistd.h>
> #endif /* WIN32 */
>
> ...
>
> /* define the function to use */
> #ifdef WIN32
> #define SLEEP_TIME 5000
> #define SLEEP_FUNC Sleep
> #else /* WIN32 */
> #define SLEEP_TIME 5
> #define SLEEP_FUNC sleep
> #endif /* WIN32 */
>
> ...
>
> /* someone deep in the code the function is used */
> if( wait_for_something == true )
> {
> SLEEP_FUNC ( SLEEP_TIME );
> }
>
> ***CODE EXAMPLE***
>
> Of course this may not be the safest or best way to do it but it works
> for simple programs.


That will work (I presume) if you happen to compile your program on a
Windows or Unix-like system. It's not portable to any other systems,
so it's off-topic here.

Also, why do you write

if( wait_for_something == true )

? Where is the identifier "true" defined? If you're using C99, it's
in <stdbool.h>; if you're using C++, it's keyword, but C++ is
off-topic here in comp.lang.c. (I just noticed that this is
cross-posted to comp.lang.c and comp.lang.c++; that's hardly ever a
good idea.)

If a variable represents a condition, it's far better to use it
directly as a condition:

if (wait_for_something)
{
...
}

Consider what happens if wait_for_something is an int with a value
other than 0 or 1.

And if you think that "wait_for_something == true" is clearer than
"wait_for_something", wouldn't "(wait_for_something == true) == true"
be better yet?

See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-27-2006
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "siska" <(E-Mail Removed)> writes:
>> Keith Thompson wrote:

> And if you think that "wait_for_something == true" is clearer than
> "wait_for_something", wouldn't "(wait_for_something == true) == true"
> be better yet?
>
> See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.


I treat that as preference. What is the difference between:
if ( wait_for_something )
and
if ( wait_for_something == true )

The biggest difference is that me, looking at this code, immediately know in
the second form that wait_for_something is a boolean value. In the first
form I don't know if it's an integer or char or perhaps even something else.
I'm one who believe in self documenting code.

Yes, I would probably write
if ( wait_for_something )
only if wait_for_something was obviously a boolean value.



 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I know about the shutdown timer, is there a "switch on" timer... Mal Carter Computer Support 3 01-12-2008 01:58 AM
page timer, or redirect timer Simon ASP .Net 1 11-04-2005 07:14 PM
System.Timers.Timer/System.Threading.Timer Not Firing At All on Dev Server james.e.coleman@gmail.com ASP .Net 1 02-22-2005 09:41 PM
old timer on net but Mozilla newbie carlton hunter Firefox 1 01-31-2004 04:53 AM
System.Timers.Timer vs. System.Threading.Timer Kelsang Wangchuk ASP .Net 0 07-31-2003 04:28 PM



Advertisments