Timer that Launches a Method at Regular Intervals #2

  • Follow


In Java one can set up a timer, and using that timer arrange for
methods to be called at regular intervals, say every five seconds.  I
understand I can do that in C++ with the <sleep()> function, but that
would tie up the CPU, wouldn't it?  And keep other functions from
running while the <sleep()> function is executing?  I want to be able
to schedule my method to run at regular intervals, but I also want it
so that other functions can run while an interval is waiting to
complete.  Is that possible with C++?  And if so, how does one do it?
I'm using VisualStudio on a Windows XP machine.

Kevin S
0
Reply kvnsmnsn (147) 11/3/2010 4:10:53 PM

On 03/11/2010 16:10, KevinSimonson wrote:
> In Java one can set up a timer, and using that timer arrange for
> methods to be called at regular intervals, say every five seconds.  I
> understand I can do that in C++ with the<sleep()>  function, but that
> would tie up the CPU, wouldn't it?  And keep other functions from
> running while the<sleep()>  function is executing?  I want to be able
> to schedule my method to run at regular intervals, but I also want it
> so that other functions can run while an interval is waiting to
> complete.  Is that possible with C++?  And if so, how does one do it?
> I'm using VisualStudio on a Windows XP machine.

You use a timer :)
Look at the SetTimer API call.

-- 
Dee Earley (dee.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)
0
Reply Dee 11/3/2010 4:25:04 PM


KevinSimonson wrote:
> In Java one can set up a timer, and using that timer arrange for
> methods to be called at regular intervals, say every five seconds.  I
> understand I can do that in C++ with the <sleep()> function, but that
> would tie up the CPU, wouldn't it?

There is a Sleep() function in the win32 API, which stalls execution of the
current thread for a certain time, which you could use. It stalls the
current thread by giving the CPU to other threads/processes, so it
doesn't "tie up the CPU".

> And keep other functions from running while the <sleep()> function
> is executing?

Other threads, in the same or different processes, are not affected by this.

> I want to be able to schedule my method to run at regular intervals, but
> I also want it so that other functions can run while an interval is
> waiting to complete.  Is that possible with C++?

There are three ways:
1. You regularly look at the time and call the according functions you want
when they are due. The downside is that you have to do so manually and it
requires changes to other parts of your code. If you forget to poll in a
long-running operation, your functions also won't be called.
2. You create a thread that calls the functions. This works completely
separately, but requires synchronisation when accessing shared data etc.
Multithreaded programming is very useful but for a beginner it is just
another can of worms which they may not want to open.

Using SetTimer as Dee suggested is basically variant 1, only that a
framework is provided by Windows itself. This framework is used to send
events to programs with a GUI. It requires that you use an event loop
(called "message loop" for win32 programming) and that you structure your
program accordingly. If you do so already, this is probably the best way to
go.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

0
Reply Ulrich 11/3/2010 4:59:36 PM

Thanks for this information!  It was precisely what I needed.

Kevin S

0
Reply KevinSimonson 11/3/2010 9:50:22 PM

On Nov 3, 9:10=A0am, KevinSimonson <kvnsm...@hotmail.com> wrote:

> In Java one can set up a timer, and using that timer arrange for
> methods to be called at regular intervals, say every five seconds. =A0I
> understand I can do that in C++ with the <sleep()> function, but that
> would tie up the CPU, wouldn't it?

Nope. The CPU is completely free to do other things. It will, however,
tie up the thread. But if you don't want the thread to sleep, don't
call 'sleep'.

>=A0And keep other functions from
> running while the <sleep()> function is executing? =A0I want to be able
> to schedule my method to run at regular intervals, but I also want it
> so that other functions can run while an interval is waiting to
> complete. =A0Is that possible with C++? =A0And if so, how does one do it?

Use the following code:

1) Is there something we already know we need to do now? If so, do it.
Go back to step 1.
2) Is there something we scheduled to do later that it is now time to
do? If so, do it. Go back to step 1.
3) Wait until either something happens or it is time for us to do
something. Then go to step 1.

> I'm using VisualStudio on a Windows XP machine.

You can also use threads or Windows built-in platform-specific timer
support.

DS

0
Reply David 11/4/2010 4:09:48 AM

4 Replies
586 Views

(page loaded in 0.052 seconds)

Similiar Articles:













7/20/2012 2:00:00 PM


Reply: