Solved

Timers in c++

Posted on 2004-09-17
6
320 Views
Last Modified: 2012-06-21
I'm just new to C/C++ and I've searched the web for a timer function.
I'm coding a console app that will set a timer running for 25 mins and get it to execute an external application after the time is up. (basically i need a console version of TTimer in borland c++ builder).
Could i have info on timers and execute functions please??

GE
0
Comment
Question by:Grand_Edgemaster
  • 2
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
AaronReams earned 50 total points
ID: 12089526
I'm not sure if there is a better way but you could easily do the following.

1) launch a separate thread (pass pointer to data structure if needed)

2) have that thread call Sleep(25*60*1000);  

3) have that thread launch desired application

4) then thread returns and exits.

let me know if this sounds good to you and if you need more help.

cheers,
aaron
0
 

Author Comment

by:Grand_Edgemaster
ID: 12089546
I'll look into it
Thanks for the quick response
0
 
LVL 8

Expert Comment

by:AaronReams
ID: 12089551
no problem and to execute your external application you'll probably want to use one of the _spawn functions.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__spawn.2c_._wspawn_functions.asp
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12090662
If your console prog already is running an infinite loop you may count the time by calls to GetTickCount() (assuming you are on a Windows platform, though there are equal functions on other platforms also).

void main()
{

    long    l1 = GetTickCount();
    bool    chkTimer = true;
    while (true)
    {
         // check timer
        if (chkTimer)
        {
             l2 = GetTickCount();
             if (l2 - l1 >= 25*60*1000)
             {
                   ShellExecute(...);
                   chkTimer = false;
             }  
        }

        // do other stuff

        ...

       Sleep(10);  // gives other applications cpu time
    }
}

On NT, 2k and XP systems you also could use CreateWaitableTimer function. That gives a handle where you can wait for using WaitForSingleObject  or WaitForMultipleObjects.

Regards, Alex
0
 

Author Comment

by:Grand_Edgemaster
ID: 12309764
Sorry for the delay in reply,
I solved the problem myself in the end by just reverting back to my original GUI program and using TTimer. However, I didn't find many of the above comments helpful (i also already knew about the spawn functions). Thanks for all your help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

758 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