Today's Smalltalk 4 You continues looking at the basic Smalltalk class libraries in VA Smalltalk - today it's the Delay and Duration classes. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:
You can also watch it on YouTube:
Today we'll look at class Delay and Duration. You'll need these classes anytime you want to set up a timer of some kind in VA Smalltalk (such as in a process that wakes up periodically to do some task). To get started, we want to look at the two classes:
You can use Delay without using Duration; Duration makes it easy to specify a time interval (by second, millisecond, day, or even longer time periods). That makes it easy to specify an interval, and then set up a Delay to wait for that interval. You do that via the #wait message:
"use a duration" duration := Duration seconds: 10. ms := duration asMilliseconds. Transcript show: 'Now: ', DateAndTime now asMilliseconds printString; cr. (Delay forMilliseconds: ms) wait. Transcript show: 'End: ', DateAndTime now asMilliseconds printString; cr.
What we did above is specify a duration, and then use that duration as the interval for the Delay. If you look in your Transcript afterwards, you'll see the following:
You can simply specify a number of seconds or milliseconds in Delay, without using a Duration; Duration just makes it easy to set up a time interval:
"use seconds" Transcript show: 'Now: ', DateAndTime now printString; cr. (Delay forSeconds: 5) wait. Transcript show: 'End: ', DateAndTime now printString; cr.
Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.
[st4u182-iPhone.m4v ( Size: 4936586 )]