We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't get LDTimers to work
#1
LitDev,

Here's the code:

'Timer.interval= 10
'Timer.tick=OnTimerTick

'Name=LDTimer.Addtick(OnTimerTick)
'LDTimer.Interval(Name,10)

Name=LDTimer.Add()
LDTimer.Interval(Name,10)
LDTimer.Tick=OnTimerTick

Sub OnTimerTick
  TextWindow.WriteLine("Here")
EndSub

The first 2 lines are the regular timer. That works, but it has been commented out. The next 2 lines have been commented out and they don't work. The next 3 have not been commented out, but they don't work either. I couldn't find this in the old forum. So, how do you make these work. To me they are setup like the first 2 lines which do work.

JR
Reply
#2
LitDev,

I did get this to work by putting in a while program delay loop. Here's the code:

'Name=LDTimer.AddTick("OnTimerTick")
'LDTimer.Interval(Name,0)

Name=LDTimer.Add()
LDTimer.Interval(Name,0)
LDTimer.Tick=OnTimerTick

While "True"
Program.Delay(0)
EndWhile

Sub OnTimerTick
TextWindow.WriteLine("Here")
EndSub

Notice there isn't any actual delay, but the program keeps on running. So, originally it doesn't have enough time for the tick event to occur, but with the SB timer it is able to do it. Interesting!

JR

My question now is does each of the ldtimers run on a separate thread?
Reply
#3
JR,

Did you realize the the interval is defined in miliseconds, so the program should print 100 lines  per second?
I havn't tried this but waht happens when you take an interval of 10000 ?

EDIT:
I trested the following program, this works:

Name=LDTimer.Add()
LDTimer.Interval(Name,5000)
LDTimer.Tick=OnTimerTick

while 0=0
  Program.Delay(1000)
  TextWindow.WriteLine(" in while")
  EndWhile

Sub OnTimerTick
  TextWindow.WriteLine("Here")
EndSub
[-] The following 1 user Likes WhTurner's post:
  • jrmrhrb00
Reply
#4
JR,

You are right that the LD version takes a little longer to get going - not quite sure why this is, but there is more code due to potentially several timers.

A continuous loop isn't needed, since SB stays running when TextWindow is open, so the following also work.

Code:
Name=LDTimer.Add()
LDTimer.Interval(Name,10)
LDTimer.Tick=OnTimerTick

Program.Delay(100)

Sub OnTimerTick
  TextWindow.WriteLine("Here")
EndSub

or

Code:
Name=LDTimer.Add()
LDTimer.Interval(Name,10)
LDTimer.Tick=OnTimerTick

TextWindow.WriteLine("Start")

Sub OnTimerTick
  TextWindow.WriteLine("Here")
EndSub

Each timer does use its own thread.  The following code trys to show how the different timers can be used.

Code:
'Original timer
Timer.interval= 1000
Timer.tick=OnTimerTick1

'2 Timers sharing an event subroutine
LDTimer.Tick=OnTimerTick2
Name1=LDTimer.Add()
LDTimer.Interval(Name1,500)
Name2=LDTimer.Add()
LDTimer.Interval(Name2,600)

'2 Timers on different event subroutines
Name3=LDTimer.Addtick("OnTimerTick3")
LDTimer.Interval(Name3,700)
Name4=LDTimer.Addtick("OnTimerTick4")
LDTimer.Interval(Name4,800)

Sub OnTimerTick1
  TextWindow.WriteLine("SB Timer1")
EndSub

'2 timers sharing subroutine, distinguished by LastTimer
Sub OnTimerTick2
  timer = LDTimer.LastTimer
  TextWindow.WriteLine("LD Timer2 "+timer)
EndSub

'Since these timers are on different subroutines they don't set LastTimer
Sub OnTimerTick3
  TextWindow.WriteLine("LD Timer3")
EndSub

Sub OnTimerTick4
TextWindow.WriteLine("LD Timer4")
EndSub

Just saw last post by WHTurner and for the reasons above this also works.

Code:
Name=LDTimer.Add()
LDTimer.Interval(Name,5000)
LDTimer.Tick=OnTimerTick

TextWindow.WriteLine("Start")

Sub OnTimerTick
  TextWindow.WriteLine("Here")
EndSub
[-] The following 1 user Likes litdev's post:
  • jrmrhrb00
Reply
#5
LitDev and WHTurner,

I appreciate both of your comments. I especially liked the code on how to use multiple timers.

Thanks,

JR
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)