Solved

Timers in c++

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

Technology Partners: 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

734 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