[Newbies] Background method in squeak

sebovick sebovick at gmail.com
Fri Nov 19 14:11:20 UTC 2010

Le 15/11/2010 12:00, Bert Freudenberg a écrit :
> On 14.11.2010, at 23:07, David T. Lewis wrote:
>> On Sun, Nov 14, 2010 at 12:46:00AM +0100, seb wrote:
>>> Hello,
>>> I want to start a methods at the creation of an object, and every second give
>>> me status of somme of his variable. When i create the instance I can see in
>>> the Transcript the output expected. But the squeak interface freeze (and
>>> processor goes to 100%). How can I make this method execute in background?
>>> Here is my buggy method:
>>> MyClass>>beeper
>>> 	 [ self beeperOn ] whileTrue: [
>>> 		Transcript show: self myStatus; cr.
>>> 		( Delay forSeconds: 1) wait.
>>> 		]
>> S?bastien,
>> This is a good question, and the answer is not at all obvious. Squeak uses
>> threads of execution called Processes. The things that interact with the
>> user interface are expected to run within the single process that handles
>> the user interface. User interface objects require this (it is an intentional
>> design decision, not a bug), so if you start your own background process
>> that needs to interact with the UI, the background process needs to schedule
>> its UI related activities into the main UI process. This is done with the
>> #addDeferredUIMessage: method. You can look for senders of #addDeferredUIMessage:
>> for examples, but I think that the following will do what you want:
>> MyClass>>beeper
>>    [ self beeperOn ] whileTrue: [
>>        Project current addDeferredUIMessage: [Transcript show: self myStatus; cr].
>>        ( Delay forSeconds: 1) wait.
>>        ]
>> Dave
> Yes, but this is only the second step. You also have to execute that method in a different process first. If you send
> 	myInstance beeper
> then this will be executed in the current process, which normally is the UI process. It will block until the method returns.
> To make it execute in the background in a new process, you have to fork the execution:
> 	[myInstance beeper] forkAt: Processor userBackgroundPriority
> Note however, that this will only be executed while the main process is idle. That's because "userBackgroundPriority" is lower than the normal priority (userSchedulingPriority).
> If you want this new process to interrupt the main process briefly every second (which I guess you do) then use a higher priority, e.g.:
> 	[myInstance beeper] forkAt: Processor userInterruptPriority
> Be advised though that programming with processes is not considered a beginner's topic in Squeak. Not because it's especially hard (as you can see above) but because it rarely is needed for beginner-type (and even most advanced) programs. There are usually less error-prone ways to achieve what you want.
> E.g. in the Morphic UI everything seems to execute in parallel. You can have animations running and you are still able to interact with all the windows and tools. But it is all just one process.
> - Bert -
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

Thanks for your responses. If I understand well, this is about the UI 
process. Does this mean that if I launch my image without UI method like 
this will work as expected?

Thanks again.


More information about the Beginners mailing list