?
Solved

Timers in c++

Posted on 2004-09-17
6
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
AaronReams earned 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

764 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