Page 1 of 1

Freezing Repeating Loop Not Tied to Button

Posted: Thu Dec 03, 2015 4:50 am
by newpie
Hello I wanted to ask if someone could look at this code. I keep getting a frozen stack and unaware of the short keys to kill the stack in this situation -->My second question :)

Goal: To run an "update" handler every 15 min once they enter and have a certain card active.

Issue: What I noticed is it doesn't wait the "10" seconds. The answer box comes back within milliseconds of pressing the OK. Then it freezes livecode.

Code: Select all

On PreOpenCard
   UpdateCheck
end PreOpenCard

on UpdateCheck
repeat
   put the short name of this card into theCard
   if theCard = "TargetCard" then
      updateCARD --handler that does items that is located on stack script as I call it in other handlers as well
      send "UpdateCheck" to me in 10 seconds --this would normally be 600 seconds
      answer "Here"
   end if
end repeat
end UpdateCheck
Any help would be appreciated as this kind of code logic would be useful to me on many levels. Thanks

Third question: Do you recommend me clearing any pending messages as well within this script to prevent memory leaks:

Code: Select all

on MyTimer
   DoSomethingPeriodically
   if MyTimer is not in the pendingMessages then
      send "MyTimer" to me in 1 second
   end if
end MyTimer
http://forums.livecode.com/viewtopic.php?f=9&t=15480

I used this post to help me get started, fyi.
http://forums.livecode.com/viewtopic.php?f=7&t=24243


Thank you for any help in these matters.

Re: Freezing Repeating Loop Not Tied to Button

Posted: Thu Dec 03, 2015 5:55 am
by quailcreek
Hi newpie,
You're not telling your repeat when to stop. Repeat for each line theLine in theContainer. Repeat until... Repeat the X = 1 to 20...

Re: Freezing Repeating Loop Not Tied to Button

Posted: Thu Dec 03, 2015 6:15 am
by newpie
Thanks for the quick reply. I actually don't want it to stop, just wait around 10 min between each iteration, unless they exit the program or leave the "target card". Its main purpose is to update a datagrid against an external database.

Re: Freezing Repeating Loop Not Tied to Button

Posted: Thu Dec 03, 2015 8:18 am
by SparkOut
You don't want the repeats at all. The update check handler calls itself recursively with the "send" timer, and will therefore continue as long as the conditions are met to send the next message. You could make this more general and probably robust by having the condition in the timed loop check for an "IsRunning" flag you set. For example on openCard of "TargetCard" you could set IsRunning to true and send "UpdateCheck" to this card/ stack. On closeCard you can cancel pending messages and set IsRunning to false.

Re: Freezing Repeating Loop Not Tied to Button

Posted: Thu Dec 03, 2015 12:57 pm
by WaltBrown
newpie,
If it's the "answer here" line, it executes immediately after the send command posts the new message, not AFTER the 10 seconds. "Send" does not block.

And like SparkOut says, you don't need the repeat. What's happening is that repeat is just spinning like mad (without a stop, as quailcreek says) posting thousands of new update messages, freezing LC, and then crashing out when you hit some buffer limit. And if the messages even come back, they just start new infinite repeat loops.

Walt

Re: Freezing Repeating Loop Not Tied to Button

Posted: Fri Dec 04, 2015 12:31 am
by newpie
Thank you all for helping me understand that concept. I will work on some new coding.