Eric Clack
Eric Clack

Reputation: 1926

How do I sleep for a few seconds in Smalltalk Pharo, and be able to interrupt this?

I'm debugging some keyboard event code and I want to loop with a sleep (to give me a chance to create the keyboard event), however when I do this Pharo won't let me quit with Command-. so debugging is difficult. I had to wait 500 seconds to fix something in the code below...

100 timesRepeat: [ 
    Transcript show: 'Type an a... '.
    (Delay forSeconds: 5) wait.
    (Sensor keyPressed: $a) ifTrue: [ Transcript show: 'you pressed a' ].
]

So how can I make Command-. work, or is there something more suitable than (Delay forSeconds: 5) wait.?

Upvotes: 5

Views: 2322

Answers (3)

Melvin Roest
Melvin Roest

Reputation: 1492

I just started with Pharo and it seems what you're really running into is still an issue among beginners (myself included). Looking at your code it seems you want to the Transcript to update every 5 seconds. Here is how to do it (comments included to make certain nuances clear).

| process | "If you're running outside a playground, you should declare the variable, otherwise you should not declare it because it needs to bind to the playground itself"

process := [ 
    100 timesRepeat: [ 
        Transcript show: 'Type an a... '; cr. "I like a newline, hence the cr"
        (Delay forSeconds: 5) wait.
        "In Pharo 10, the following doesn't work, still need to figure out how to do this"
        "(Sensor keyPressed: $a) ifTrue: [ Transcript show: 'you pressed a' ]."
    ]
] fork.

process terminate. "You can run this to terminate the process inside the playground"
process suspend. "Also possible"
process resume. 

Upvotes: 0

Rialgar
Rialgar

Reputation: 753

I am not entirely shure this works in Pharo, but in Squeak you can just fork your code in a new process, so it does not block the UI:

[
    100 timesRepeat: [ 
        Transcript show: 'Type an a... '.
        (Delay forSeconds: 5) wait.
        (Sensor keyPressed: $a) ifTrue: [ Transcript show: 'you pressed a' ].
    ].
] fork.

Upvotes: 1

codefrau
codefrau

Reputation: 4623

Works fine in Squeak on Mac OS X (using peekKeyboardEvent, it does not have keyPressed:). So it's not your code's fault, interrupting this should work fine.

Upvotes: 1

Related Questions