[squeak-dev] How do I "sleep 5"?

Marcel Taeumel marcel.taeumel at hpi.de
Fri Feb 24 09:51:05 UTC 2023


Hi Vanessa --

Thanks for the explanation. :-)

Behind the scenes, Morphic alarms make use of a shared queue to synchronize back into the UI process. MVC has this as well:

MorphicProject >> #addDeferredUIMessage:
MVCProject >> #addDeferredUIMessage:

So, what you would do if you would want to use #fork at some point is this:

| currentProject |
currentProject := Project current.
[ 5 seconds wait.
currentProject addDeferredUIMessage: [ "do_something" ] ] fork.

Luckily, years ago, we fixed some scheduling-related issues so that a regular #fork from within Morphic's UI process should work without #addDeferredUIMessage: as well. That is, a simple #fork will create a process with the same priority as the Morphic UI process (here: 40). And after the 5-second delay fires, that forked process will be scheduled to run the next time Morphic's main loop has finished its current cycle (see: WorldState >> #doOneCycleFor:), which means that chances are good that your forked code will not disrupt anything in Morphic:

[ 5 seconds wait. "do_something" ] fork. "40 -> Project current uiProcess priority"

[ 5 seconds wait. "do_something" ] forkAt: Project current uiProcess priority.

Yet, I would not consider this idiomatic Morphic code. Also it does not work if preemption would yield that (UI) process, which would be the case for that firing delay if:

Smalltalk processPreemptionYields.


By default, process preemption does not yield, which means that no delay can "interrupt" (or interfere with) Morphic's UI process.

Best,
Marcel
Am 23.02.2023 22:55:07 schrieb Vanessa Freudenberg <vanessa at codefrau.net>:
Because Morphic is not multi-thread-clean, you have to be really careful how to write the do_something to be reliably executed from a different process. You have to coordinate with the UI process. In which case it actually becomes simpler to just stay in the UI process and use the mechanisms available.

Vanessa

On Thu, Feb 23, 2023 at 11:38 AM Stephen Travis Pope <stephen at heaveneverywhere.com [mailto:stephen at heaveneverywhere.com]> wrote:


I’ve been watching this thread and wondering why nobody suggests the simplest answer:

[(Delay forSeconds: 5) wait.
do_something] fork.

stp


--------

Stephen Travis Pope    Ojai, California, USA
     [pastedGraphic.tiff]
     http://HeavenEverywhere.com [http://HeavenEverywhere.com]
     http://FASTLabInc.com [http://FASTLabInc.com]
     https://vimeo.com/user19434036/videos [https://vimeo.com/user19434036/videos]
     http://heaveneverywhere.com/Reflections [http://heaveneverywhere.com/Reflections]



On Feb 22, 2023, at 11:16 AM, karl ramberg <karlramberg at gmail.com [mailto:karlramberg at gmail.com]> wrote:



On Wed, Feb 22, 2023 at 8:47 AM Taeumel, Marcel via Squeak-dev <squeak-dev at lists.squeakfoundation.org [mailto:squeak-dev at lists.squeakfoundation.org]> wrote:

(bm2 future: 1000) doButtonAction.

Also works for objects that are not or cannot be in the world.

Uses Morphic alarms when when in UI process.
 
Ah, that's cool. I never used #future:  :-)

Best,
Karl
 


Best,
Marcel
From: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org [mailto:squeak-dev-bounces at lists.squeakfoundation.org]> on behalf of Eduardo Ochs <eduardoochs at gmail.com [mailto:eduardoochs at gmail.com]>
Sent: Wednesday, February 22, 2023 7:17:01 AM
To: The general-purpose Squeak developers list <squeak-dev at lists.squeakfoundation.org [mailto:squeak-dev at lists.squeakfoundation.org]>
Subject: Re: [squeak-dev] How do I "sleep 5"?
 
Hi Karl!
Fantastic, thanks! =)
I added this to the class in which I'm putting most of my stuff,

!See class methodsFor: 'as yet unclassified' stamp: 'Edrx 2/22/2023 02:54'!
run: aBlock after: ms
| aButton |
aButton := SimpleButtonMorph new.
aButton openInWorld;
        hide;
        target: aBlock;
        actionSelector: #value;
        addAlarm: #doButtonAction after: ms.
! !

and now I can simply run this

  See run: [ kf := self currentHand keyboardFocus ] after: 5000.

to save into the variable kf the morph on which the keyboard focus is
after 5 seconds. Neat! =)

  Cheers,
    Eduardo Ochs
    http://anggtwu.net/eev-squeak.html [http://anggtwu.net/eev-squeak.html]



On Wed, 22 Feb 2023 at 01:56, karl ramberg <karlramberg at gmail.com [mailto:karlramberg at gmail.com]> wrote:

Hi,
A morph has to be in the world to be able to interact with it; eg. #openInWorld.


If you don't want to see the morph you can send it message #hide. It makes the morph invisible but it's still in the world and can interact with it.

To see the morph again send #show.
To delete the morph send #delete. The morph will be garbage collected.


Best,
Karl



On Tue, Feb 21, 2023 at 11:57 PM Eduardo Ochs <eduardoochs at gmail.com [mailto:eduardoochs at gmail.com]> wrote:

Hi Vanessa!
Thanks! =) This works in a workspace,

  "Create a SimpleSwitchMorph with label 'Toggle'
      and a SimpleButtonMorph with label 'Flash'.
      The button will be placed below the switch."
 
  sm := SimpleSwitchMorph new.
  sm openInWorld.
  bm := SimpleButtonMorph new.
  bm openInWorld.
  bm position: bm position + (0 at 32).
 
  "Three ways of toggling the color of the switch:"
 
  sm toggleState.
 
  bl := [ sm toggleState ].
  bl value.
 
  bm target: bl.
  bm actionSelector: #value.
  bm doButtonAction.
 
  "Two ways of toggling the switch after 1000ms:"
 
  sm addAlarm: #toggleState after: 1000.
  bm addAlarm: #doButtonAction after: 1000.

but this doesn't:

  bm2 := SimpleButtonMorph new.
  bm2 target: bl.
  bm2 actionSelector: #value.
  bm2 addAlarm: #doButtonAction after: 1000.

What is the right way to add an alarm to a morph that is not shown on
the screen? Also, can I create a new invisible morph every time that I
want to run an alarm? Are they going to be garbage collected?

  Thanks in advance!
    Eduardo Ochs
    http://anggtwu.net/eev-squeak.html [http://anggtwu.net/eev-squeak.html]



On Tue, 21 Feb 2023 at 02:16, Vanessa Freudenberg <vanessa at codefrau.net [mailto:vanessa at codefrau.net]> wrote:

The best way to do this in Morphic is with "alarms":

    self addAlarm: #changeKeyboardFocus after: 5000.


which would execute the morph's changeKeyboardFocus method 5 seconds later.

The way of sleeping you suggest is possible too but more tricky, since you would have to move your wait code to an extra process to not block the UI process, but then make sure that the actual work is done in the UI process again (Morphic is not multithreaded, although Squeak is).

Vanessa

On Mon, Feb 20, 2023 at 8:49 PM Eduardo Ochs <eduardoochs at gmail.com [mailto:eduardoochs at gmail.com]> wrote:

Hi list,

a few days ago I asked for help on how to send a "synthetic" keyboard
event to a morph, and Karl Ramberg gave me exactly the right hints in
this thread:

http://lists.squeakfoundation.org/pipermail/squeak-dev/2023-February/223473.html [http://lists.squeakfoundation.org/pipermail/squeak-dev/2023-February/223473.html]

My code is ready except for documentation and comments - I'll work on
that in the next few days and then post the result here and on the
wiki...

...but there's a feature that I want to add to it that - again =( -
needs something that I'm not being able to discover by myself. How do
I write a line of code that waits for 5 seconds, sort of like running
"sleep 5" in a shell, and that doesn't block the rest of the system?
If I have that I'll be able to run these two lines in a workspace,

  self mySleep: 5000.
  kf := self currentHand keyboardFocus.

switch the keyboard focus to something else by clicking on it, and
then the variable kf will be set to this "something else"...

Thanks in advance!
  Eduardo Ochs
  http://anggtwu.net/eev-squeak.html [http://anggtwu.net/eev-squeak.html]









-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20230224/4afc725f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pastedGraphic.tiff
Type: image/tiff
Size: 2442 bytes
Desc: not available
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20230224/4afc725f/attachment.tiff>


More information about the Squeak-dev mailing list