ludo
ludo

Reputation: 553

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']

Upvotes: 1

Views: 273

Answers (2)

fede s.
fede s.

Reputation: 592

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.

Upvotes: 3

draegtun
draegtun

Reputation: 22570

|span aDate|

aDate := DateAndTime year: 2012 month: 12 day: 12.
span := Timespan starting: aDate duration: 1 minute.
span every: 10 seconds do: [ :each | Transcript show: each; cr ].

Output:

2012-12-12T00:00:00+00:00
2012-12-12T00:00:10+00:00
2012-12-12T00:00:20+00:00
2012-12-12T00:00:30+00:00
2012-12-12T00:00:40+00:00
2012-12-12T00:00:50+00:00

Upvotes: 1

Related Questions