challenge 出力の一時停止と再開

起動すると、標準出力に1秒毎に'a'の1文字を出力し続けるプログラムで、 以下の条件を満たすものを「どう書く?」

  • 'q'キーが押されるとプログラムは終了する
  • 出力中に'p'キーが押されると一時停止する
  • 一時停止中に'p'キーが押されると出力を再開する

Posted feedbacks - Smalltalk

Squeak Smalltalk で。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
| process |
process := [
    World findATranscript: nil.
    [   Transcript show: $a.
        (Delay forSeconds: 1) wait
    ] repeat
] fork.

[   ActiveHand checkForMoreKeyboard ifNotNilDo: [:event |
        event keyCharacter = $q ifTrue: [^process terminate].
        event keyCharacter = $p ifTrue: [
            process isSuspended
                ifTrue: [process resume]
                ifFalse: [process suspend]]].
    Processor yield.
] repeat

Index

Feed

Other

Link

Pathtraq

loading...