![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
timed functions
Is there a way to run a function every so many seconds. Like lets say i have a function that updates an array and i want it to update every 200 ms. Is there a way to say run functionX every 200 ms? It doesn't have to be a standard c++ thing it could be a windows only thing. The reason i am asking is i have a 2d int array that i want to move a number from the top to the bottom (so the y axis) in a time interval. So i have a function that creates the number based on the y value you give it now i just need to know how to call that function every 200 ms or any interval of time would work and the number will move down the array. Thanks.
Last edited by cwl157; Jun 20th, 2008 at 12:39 PM. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Re: timed functions
Not in standard C/C++.
Most modern systems, however, have some form of timer mechanism that allows a message/signal to be sent to your program at some defined interval, and your program can respond to that message/signal. This sort of thing is more usually supported in event-based GUI frameworks than in console applications. Catch is, most operating systems or GUI frameworks support this in different ways. Another (less commonly used) alternative is use of another thread that manually triggers a message or signal to the main thread of the program. Support of multithreading is also not part of standard C/C++ (although a subject of discussion for future standards). Either way, you're going to have to read up on library functions for your operating system and/or GUI framework. Another thing to keep in mind is that, with most operating systems, the interval between such events (however you trigger them) is subject to considerable variation and that variation is undocumented. A specified 200ms might give you an actual interval of 200 +/- 100 milliseconds, depending on operating system and system load, for example. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: timed functions
i was reading some google searches and found some stuff about ctime and the clock() and delay() functions. Could those be used to do what i want?
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
Re: timed functions
not for accuracy < 1 second.
Look up gettimeofday() for linux, and GetSystemTimeAsFileTime for windows. They should have what you are after. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Re: timed functions
clock() also returns approximate values of processor time, in an implementation defined manner, not elapsed time. The relationship between processor time and elapsed time depends on things like processor speed and is affected by other system properties (eg other programs running that also use processor resources).
The time() function (which returns a value that can be fed to asctime()) has, at best, a resolution of one second. delay() is not a standard function. In principle it is possible to use a loop that retrieves time repeatedly, until your desired interval has lapsed, before calling your wanted function. Such timing loops tend are typically viewed as unfriendly, particularly in modern multi-tasking operating systems, because they steal processor resources away from other running programs. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| binding between ordinary member functions & polymorphic member functions | ASH | C++ | 2 | May 11th, 2006 4:36 AM |
| time Delays and Random functions | Markphaser | C++ | 17 | Feb 21st, 2006 3:48 AM |
| Use C++ functions is VB.NET | userName123 | Visual Basic .NET | 1 | Oct 8th, 2005 8:32 AM |
| Exporting Functions | victorsk | Visual Basic | 1 | May 18th, 2005 2:32 PM |
| User-defined creatNode and deleteNode functions for a doubly-linked list | jgs | C | 2 | Apr 28th, 2005 8:53 AM |