Search code examples
smalltalkpharo

How do we use every: do: method of pharo?


I've looked in the pharo book and I couldn't see any examples of every:aDuration do:aBlock. I found a Timespan class which when ran does give an error when the object is created and the method is called.

|plan|
plan := Timespan new.
plan start.
" save the file every 30s"
plan
   every:30 seconds do:[ Transcript show:'My message']


Solution

  • Timespan's every:do: doesn't schedule events. You might try forking a background process with a Delay to do that.

    I have only Squeak handy at the moment, but it should be more or less the same.

    Something like this:

    planProcess := [
                     [ 30 seconds asDelay wait.
                       Transcript show: 'Saved (but not really)'; cr.
                     ] repeat.
                   ] fork.
    

    To end the process:

    planProcess terminate.
    

    There's also a Scheduler you could use for this.