Adding more Smalltalk code
As you have seen, much of the code needed to implement the timer part was generated for you. What is missing is the logic that implements the start and stop actions. Before you write that code, add one more instance variable to keep track of the operating system's timer after it is started.
1. If a method is selected in the methods list, hold down the Ctrl key and click on the method name to deselect it. This displays the class definition in the Script Editor.
2. Modify the class definition by adding the instance variable timer. Save the new definition by selecting Save from the text area's pop-up menu.
The class definition should look like the following:
AbtAppBldrPart subclass: #MyTimer
instanceVariableNames: 'repeat length timer'
classVariableNames: ''
poolDictionaries: ''
Now you can implement the start and stop instance methods. From the Script Editor, ensure that the Instance button is active, then, in the right pane, select New Method Template from the pop-up menu.
start
"Perform the start action."
timer := CwAppContext default
addTimeout: self length
receiver: self
selector: #eventTimerFired:
clientData: nil.
stop
"Perform the stop action."
timer notNil ifTrue: [CwAppContext default
removeTimeout: timer ].
The CwAppContext class is in the Common Widgets subsystem. Its removeTimeout and addTimeout:receiver:selector:clientData: methods provide the necessary timing function by adding and removing an operating system timer.
Finally, write the following eventTimerFired: instance method, which is called whenever the timer expires. Because VA Smalltalk did not generate a default script for this method, from the Methods menu select New Method Template to create it. The following method signals the timerFired event:
eventTimerFired: anObject
"Notify other parts that the timer has expired."
self signalEvent: #timerFired.
timer := nil.
self repeat ifTrue: [self start].
Now you have finished building the timer part. Save the part and close the Composition Editor. After you save the part, the next step is testing.
Last modified date: 01/12/2017