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: Timer that Launches a Method at Regular Intervals - comp.os.ms ...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 ... 128-bit MMX versus 32-bit memory copy - comp.lang.asm.x86 ...Timer that Launches a Method at Regular Intervals - comp.os.ms ... 128-bit MMX versus 32-bit memory copy - comp.lang.asm.x86 ..... is anywhere from 33% to 20% of what I ... Calling a method - comp.lang.java.helpTimer that Launches a Method at Regular Intervals - comp.os.ms ... I want to be able to schedule my method to run at regular intervals, but I also ... setting timer - comp.unix.programmerTimer that Launches a Method at Regular Intervals - comp.os.ms ... In Java one can set up a timer, and using that timer arrange for methods to be called at regular ... How do get a timer in a thread? - comp.unix.programmerTimer that Launches a Method at Regular Intervals - comp.os.ms ... In Java one can set up a timer, and using that timer arrange for methods to be ... is a Sleep() function ... Best Free GUI Win32 Disasssembler ? - comp.lang.asm.x86Timer that Launches a Method at Regular Intervals - comp.os.ms ... There is a Sleep() function in the win32 API, which ... If you do so already, this is probably the best ... file opening slows down - puzzling - comp.lang.xharbourMethod of testing: I did not put up a test program, rather ... Timer that Launches a Method at Regular Intervals - comp.os.ms ... file opening slows down - puzzling ... Fast bit-reverse on an x86? - comp.dspTimer that Launches a Method at Regular Intervals - comp.os.ms ... Fast bit-reverse on an x86? - comp.dsp If the bytes are arriving with strict timing intervals ... > If ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... ntpd, boot time, and hot plugging - comp.protocols.time.ntp ...... between 7 and 20 seconds to intially set the clock ... Your method seems to me to excellent. If you do that ... retransmits to those servers are done at 1-second intervals ... Timer that Launches a Method at Regular Intervals - comp.os.ms ...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 ... Timer (Java 2 Platform SE v1.4.2)Subsequent executions take place at approximately regular intervals, separated by ... Note that calling this method from within the run method of a timer task that was ... 7/20/2012 2:00:00 PM
|