Solved

Timers in c++

Posted on 2004-09-17
6
324 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

679 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