how do you create a timer?

Just starting out? Need help? Post your questions and find answers here.
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Post by inc. »

As youre going to create a thread you directly could use the API way pointed by Psychopanta and me.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

inc. wrote:As youre going to create a thread you directly could use the API way pointed by Psychopanta and me.
Honestly, threads are overkill for such an easy thing as a timer. :wink:
--Kale

Image
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Post by inc. »

Kale wrote:
inc. wrote:As youre going to create a thread you directly could use the API way pointed by Psychopanta and me.
Honestly, threads are overkill for such an easy thing as a timer. :wink:
I meant: If thinking about a thread approach, do better use the API instead of a thread. (my english = my fault ;) )
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Just found this topic, warming it up a bit:
srod wrote:You'll need to use the debugger to kill the following:

Code: Select all

Global timerquit 

Procedure Timer1(time) ;time is in milliseconds 
  Repeat 
    Delay(time) 
    Debug 1
  Until timerquit=1 
EndProcedure 

;fire the timer every second: 
CreateThread(@timer1(),1000)

Repeat
ForEver
please note that if you model a real app on this, the last REPEAT/FOREVER loop (or any other form of loop for that matter) needs an extra Delay to reduce processor load, as in:

Code: Select all

Global timerquit

Procedure Timer1(time) ;time is in milliseconds
  Repeat
    Delay(time)
    Debug 1
  Until timerquit=1
EndProcedure

;fire the timer every second:
CreateThread(@timer1(),1000)

Repeat
  Delay(1)
ForEver
I find this approach quite useful for a small simulation utility that I'm currently developing. A GUI (primary thread) is used to manipulate a number of global variables. These variables are read by the thread and used to write NMEA-like sentences to an RS232 port with speeds varying between 20 and 60 sentences/sec.

EDIT: I just tested this non-api method on Xubuntu as well. It ran without a single modification. Hurray for a portable cross platform timer solution!

Thanks SRod & TheFool!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Post Reply