Link to home
Start Free TrialLog in
Avatar of Grand_Edgemaster
Grand_Edgemaster

asked on

Timers in c++

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
ASKER CERTIFIED SOLUTION
Avatar of AaronReams
AaronReams

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Grand_Edgemaster
Grand_Edgemaster

ASKER

I'll look into it
Thanks for the quick response
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
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
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.