iterate every few minutes

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

iterate every few minutes

Post by sphere » Thu May 07, 2015 8:20 pm

Hello, i'm trying to solve a problem by playing a bit with a piece of test code.

I'm trying to repeat something every few minutes (like every 1 - 5 or 10 minutes).
I see that the options are only seconds in Livecode (or i must be overseeing something).
So i'm trying to use the time function for this

If this is working the next piece to solve is to do a repeat form in a repeat form, like reading some text files repeatedly to fill some fields and that every 1 - 5 - or 10 minutes.
also played a bit with send in time but i'm not getting it yet.

this what i got to test, but it's not doing yet what i want:

Code: Select all

global gTime
on openStack
   set the twelveHourTime to false
   put the short time into field"time"
      put the short time into gTime
   set the itemDelimiter to ":"
   put the second item of gTime into field "minutes"
   
   repeat  until gTime
  put gTime into field"newminutes"
   wait 10 milliseconds with messages
   end repeat
end openStack
Anyone a good idea?

Thanks very much !

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: iterate every few minutes

Post by dunbarx » Thu May 07, 2015 9:08 pm

Hi.

On a new card, make a field. Now make a button. In the button script:

Code: Select all

on mouseUp
startTimer
end mouseUp

on startTimer
if the optionKey is down then exit to top
put random(999) into fld 1 --this is where your task would be run
send "startTimer" to me in 60 --ticks, 1 second
end startTimer 
You can change the "in time" value to 3600, say, to get one minute, or whatever. The technique of "sending in time" has the advantage that the process is not blocking, unlike the "wait" command, which is. Do you know what this means to you?

There have been several discussions in this forum that address the accuracy of timers. I am an advocate of interrogating "the seconds" function instead of sending a message in time, since the overhead of the working handler is no longer a burden on the repetition interval. Do you see what I mean by this? If not, and if it is of importance, write back.

Craig Newman

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: iterate every few minutes

Post by sphere » Thu May 07, 2015 9:54 pm

Hi Craig,

thanks for your help on this.
I think i understand what you mean.
By using send in time, i can still do other things, click on buttons etcetera without being blocked until the loop is finished.

and 2nd using real time like seconds is more stable then these ticks, which i noticed is not stable at all. Sometimes it takes the time you set and then all of a sudden it takes a shorter interval.
At Synthmaker/Flowstone (dsprobotics) they had similair issues, because they used the windows timer, which is unstable. Therefore they introduced Ruby interpreter which can be used as a module and has an exellent stable timer.

I like it when you can do a loop or repeat and still be able to do other things.
But is there a better more stable timing possibility available in Livecode.

So for me this is unusable as it seems unpredictable when it whil loop.

Thanks,
Sphere.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: iterate every few minutes

Post by dunbarx » Fri May 08, 2015 3:35 am

Hmmm.

I think that if you interrogate the seconds, it might be more readable, but it should not be more "stable" than the ticks.

You can always use "the milliseconds" :D

Craig

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: iterate every few minutes

Post by Simon » Fri May 08, 2015 3:57 am

Hi Craig,
Do you remember hh's code from here?
http://forums.livecode.com/viewtopic.ph ... 58&p=91125

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: iterate every few minutes

Post by sphere » Fri May 08, 2015 11:11 am

Thanks!
I will check it out tonight, and hopefully this is what i can use.

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: iterate every few minutes

Post by sphere » Fri May 08, 2015 2:03 pm

Ok when i use the scripts on that page LC is giving an error that it stopped the handler to prevent hang.

also i don't get it completely, how it works.... :oops:

Thanks

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: iterate every few minutes

Post by dunbarx » Fri May 08, 2015 2:56 pm

Hi.

This is taken directly from the forum page:

Code: Select all

on mouseUp
   put the milliseconds into startingTime
   put "" into fld "f6"
   updateTimer startingTime
end mouseUp

on updateTimer startingTime
   get the milliseconds -  startingTime
   if it > 4000 then -- "4000" just runs for 4 seconds
      put "Done"
      exit updateTimer
   end if
   get  (the milliseconds  - startingTime) 
   if it < 1000 then
      put "0" & "." & char 1 of it into line 1 of fld "f6"
   else
      put char 1 of it & "." & char 2 of it into line 1 of fld "f6"
   end if
   doOtherStuff
   wait 0  with messages
   updateTimer startingTime
end updateTimer

on doOtherStuff
   put  "Current useless output is:" && random(999) && any char of "ABCDEF" into line 2 of fld "f6"
end doOtherStuff
It stops after a short while, as intended, but works fine.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: iterate every few minutes

Post by jacque » Fri May 08, 2015 4:38 pm

dunbarx wrote:It stops after a short while, as intended, but works fine.
It has to stop after a short while because it's recursive. If it ran continuously for a long time it would throw a recursion error. Your first example was better.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: iterate every few minutes

Post by dunbarx » Fri May 08, 2015 5:16 pm

Jacque.

Why is my first one better? It just has coarser timing.

Anyway, what Jacque says is true. You must always provide an exit strategy when running a handler that calls itself. LC will soon complain if this process is open-ended. The usual methods are to stop after a certain condition is met, perhaps with a user interaction if this occurs in a reasonable time, or whatever.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: iterate every few minutes

Post by jacque » Fri May 08, 2015 8:56 pm

Your first one is better because it isn't recursive and it will never hit a recursion limit. Pending messages are separate, distinct calls to a handler. Each time the handler executes, it completes in full and cleans up after itself. The second example is recursive, none of the handler iterations ever do any final housekeeping, and traces are left in the call stack in memory. Eventually the call stack fills up and you get a recursion error.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: iterate every few minutes

Post by sphere » Fri May 08, 2015 9:06 pm

Thanks to all of you, but i still don't get it.

i searched some more and found: http://forums.livecode.com/viewtopic.php?f=7&t=22501

so i'll study that and the raspberry part on this thread: http://forums.livecode.com/phpBB2/viewt ... 3&p=114149

hopefully that is getting me somewhere, the first thread is also not so stable in timing and has probably the same issue i'm searching for. the raspberry seems more stable.

did'nt expect that doing a loop every 5 or 10 minutes would be that difficult..so have to figure out some more how this exactly works

edit--also when seeing the repeat forms, like until each for while, there is one i'm missing(or not understand how to do it :roll: ), and that is repeat when...this or that is the case

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: iterate every few minutes

Post by jacque » Fri May 08, 2015 10:07 pm

did'nt expect that doing a loop every 5 or 10 minutes would be that difficult
It isn't. The method is just this: A one-time event starts the action. The event can be a mouseclick, a card opening, anything. The one-time handler sets a "flag" variable to true, which indicates that the repetitive work should happen. It then calls the actual handler that does the work.

The working handler does something -- in your case, it gets a file and parses the content so it can put data into fields. Before it does that, it checks the flag variable to see if it should actually run, and if so, does the work and then sends a message to itself to run again in a few minutes.

After that amount of time, LC will trigger the work handler again. It checks the flag variable, and if it is true it does the work and sends the message to itself again.

This process continues until something sets the flag variable to false. That can be another mouseclick, or a closeCard, or any other event. The "stop" handler sets the flag to false, and when that happens the working handler will see it when it checks the status, and will not run any more.

You will need a start handler that sets the flag to true, a stop handler that sets the flag to false (they can be the same handler with an "if" clause if you want,) and a work handler that checks the flag and does the work. Messages are sent in multiples of milliseconds, ticks, or seconds. For a 5 minute interval you would use 60*5 seconds (300 seconds.) A ten minute interval would be 60*10 (600 seconds.) You shouldn't need a repeat loop at all, that's what the "send" command is for.

Here's the whole thing:

Code: Select all

local sWorking

on startWork
  put true into sWorking
  doWork
end startWork

on stopWork
  put false into sWorking
end stopWork

on doWork
  if sWorking = true then -- check the flag variable
    put url ("file:myfile.txt") into tText -- get the text file content from disk
    -- parse tText here and fill fields with data
    send "doWork" to me in 600 seconds -- tell LC to trigger this handler again in 10 min
  end if
end doWork
You just need to put the start and stop handlers into buttons, or trigger them some other way. And you need to write the script that parses the text and puts it into fields.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: iterate every few minutes

Post by sphere » Sat May 09, 2015 3:16 pm

Aaahaaah! Now i get it. I could not understand the 2nd send it command or how to place it al in a script.

Jacque your the best !
Thanks very much for your clear and understandable explanation :)

I'm gonna play something with it and try to fit it in my script (as i now used set the repeatrate, just before a repeat). I already placed the part that should be repeated in a handler.
In that handler are some things that are also repeated because there are more fields and buttons who follow up in number and must be set according to the status that is set in the text file that is being read. This works, but i could not repeat it very well. A repeat loop in a repeat loop seems not to work (or i just don't know how to do it :oops: )
This 'send' command can really help out this way i think.

Thanks very much again Jacque and of course the others too :) !!!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”