Hello! I want to create a callback that will call my method after, say, 10 minutes. How can I do it in squeak?
TIA.
"xyz" == xyz 42 xyzz42@gmail.com writes:
xyz> Hello! I want to create a callback that will call my method after, xyz> say, 10 minutes. How can I do it in squeak?
Open a Transcript and a workspace, and execute this in the Workspace:
[(Delay forSeconds: 10) wait. Transcript show: 'done!'] fork.
Then go off and do something else for 10 seconds. :)
Am 09.10.2008 um 15:54 schrieb xyz 42:
Hello! I want to create a callback that will call my method after, say, 10 minutes. How can I do it in squeak?
[(Delay forDuration: 10 minutes) wait. self myMethod] fork
This forks off a background process, waits 10 minutes, then calls myMethod. Note that a lot of code will break when called from a background process, you will have to worry about synchronization. Especially you must not call UI code from the background (there are of course ways to allow that).
- Bert -
Am 09.10.2008 um 16:07 schrieb Bert Freudenberg:
Am 09.10.2008 um 15:54 schrieb xyz 42:
Hello! I want to create a callback that will call my method after, say, 10 minutes. How can I do it in squeak?
[(Delay forDuration: 10 minutes) wait. self myMethod] fork
This forks off a background process, waits 10 minutes, then calls myMethod. Note that a lot of code will break when called from a background process, you will have to worry about synchronization. Especially you must not call UI code from the background (there are of course ways to allow that).
Seeing three very similar responses, here is another, safer way to do it for UI stuff. In a Morph subclass, you can say
self addAlarm: #myMethod after: 10 minutes asMilliSeconds
This will call myMethod synchronously so you do not have to worry about background processes.
- Bert -
On Oct 9, 2008, at 9:07 AM, Bert Freudenberg wrote:
Am 09.10.2008 um 15:54 schrieb xyz 42:
Hello! I want to create a callback that will call my method after, say, 10 minutes. How can I do it in squeak?
[(Delay forDuration: 10 minutes) wait. self myMethod] fork
This forks off a background process
Is there a distinction between processes and threads in Squeak?
, waits 10 minutes, then calls myMethod. Note that a lot of code will break when called from a background process, you will have to worry about synchronization. Especially you must not call UI code from the background (there are of course ways to allow that).
--- Mark Volkmann
Am 09.10.2008 um 16:24 schrieb Mark Volkmann:
On Oct 9, 2008, at 9:07 AM, Bert Freudenberg wrote:
Am 09.10.2008 um 15:54 schrieb xyz 42:
Hello! I want to create a callback that will call my method after, say, 10 minutes. How can I do it in squeak?
[(Delay forDuration: 10 minutes) wait. self myMethod] fork
This forks off a background process
Is there a distinction between processes and threads in Squeak?
No. It's all "green threads" in modern parlor, but they are named "processes" for historic reasons.
- Bert -
On Oct 9, 2008, at 9:54 AM, xyz 42 wrote:
Hello! I want to create a callback that will call my method after, say, 10 minutes. How can I do it in squeak?
You could try something like
[10 minutes asDelay wait. self doSomething] fork
Breaking the above down,
the square brackets indicate a block, the same kind of thing used in conditionals, select: messages, etc.
"fork" tells the block to start running on a new thread, so statements after this one can execute without waiting the ten minutes.
"10 minutes asDelay" creates a Delay object, and sending "wait" to it waits for the appropriate amount of time.
Going further, "10 minutes" creates a Duration, and sending "asDelay" to it creates a Delay that is set up with the same amount of time as the Duration.
There are other ways to create Delays and Durations - check out the class side of Delay or Duration in a browser, as well as the "converting" category of methods for Number. (This is where the "minutes" message above comes from.)
Hope this helps, Ben Schroeder
beginners@lists.squeakfoundation.org