Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 20th, 2008, 12:15 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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.
cwl157 is offline   Reply With Quote
Old Jun 20th, 2008, 4:21 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Jun 20th, 2008, 4:53 PM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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?
cwl157 is offline   Reply With Quote
Old Jun 20th, 2008, 5:08 PM   #4
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
Re: timed functions

not for accuracy < 1 second.

Look up gettimeofday() for linux, and GetSystemTimeAsFileTime for windows. They should have what you are after.
Seif is offline   Reply With Quote
Old Jun 21st, 2008, 1:23 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC