1

次のコマンドを使用して、次のコールバックを登録できますstdin

fileevent stdin readable thatCallback

これは、updateコマンドの実行中に、でthatCallback使用可能な入力がある間、何度も評価することを意味しますstdin

で入力できるかどうかを確認するにはどうすればよいstdinですか?

4

2 に答える 2

3

コールバック内でstdinを読み取る/取得するだけです。基本的に、パターンは、 KevinKennyのfileeventの例からのこのスニペットのようになります。

proc isReadable { f } {
  # The channel is readable; try to read it.
  set status [catch { gets $f line } result]
  if { $status != 0 } {
    # Error on the channel
    puts "error reading $f: $result"
    set ::DONE 2
  } elseif { $result >= 0 } {
    # Successfully read the channel
    puts "got: $line"
  } elseif { [eof $f] } {
    # End of file on the channel
    puts "end of file"
    set ::DONE 1
  } elseif { [fblocked $f] } {
    # Read blocked.  Just return
  } else {
    # Something else
    puts "can't happen"
    set ::DONE 3
  }
}
# Open a pipe
set fid [open "|ls"]

# Set up to deliver file events on the pipe
fconfigure $fid -blocking false
fileevent $fid readable [list isReadable $fid]

# Launch the event loop and wait for the file events to finish
vwait ::DONE

# Close the pipe
close $fid
于 2012-01-05T18:50:03.677 に答える
0

この質問への回答を見ると、fconfigureを使用してチャネルを非ブロッキングモードにする方法がわかります。このTclマニュアルには、さらに多くの詳細情報があります。fconfigureのマニュアルページとvwaitのマニュアルページを確認する必要があります。

于 2012-01-05T19:46:11.173 に答える