如何在Smalltalk Pharo中睡眠几秒钟并且能够中断?

5

我正在调试一些键盘事件代码,并且我想要循环休眠(给我一个创建键盘事件的机会),但是当我这样做时,Pharo不会让我用Command-.退出,所以调试很困难。我必须等待500秒钟才能修复下面代码中的某些问题......

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

那么我该如何使Command-.生效,或者有没有比(Delay forSeconds: 5) wait.更适合的方法?

我在 Mac OS X 上运行 Pharo 2.0。 - Eric Clack
你能在3.0镜像中尝试一下吗?我认为我们最近修复了一些全局中断的问题。 - camillobruni
我会在某个时候检查3.0版本,但是我需要适用于Pharo 2.0的东西,因为这是我们正在使用的平台。是否有计划推出带有这些修复的Pharo 2.1版本? - Eric Clack
1
它在Pharo 3.0中是有效的。 - Eric Clack
1
它在最新的Pharo 2.0版本中运行 :) - Eric Clack
显示剩余2条评论
3个回答

1

我不确定这在Pharo中是否有效,但在Squeak中,您可以将代码分叉到新进程中,以便它不会阻塞用户界面:

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

1

在Mac OS X上,使用peekKeyboardEvent(而不是keyPressed:),在Squeak中运行良好。因此,这不是您的代码的问题,中断应该可以正常工作。


是的,在Squeak 4.4中它对我有效,但我需要在Pharo 2中运行的东西。 - Eric Clack

0

我刚开始使用Pharo,似乎您真正遇到的问题仍然是初学者(包括我在内)中普遍存在的问题。从您的代码来看,您想让Transcript每5秒更新一次。以下是如何做到这一点(包括注释,以澄清某些细微差别)。

| 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. 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接