TCL:无按键时循环

发布于 2024-12-03 03:39:31 字数 342 浏览 1 评论 0原文

我想运行一个 while 循环,直到 stdin 充满一个字符。

puts "Press x + <enter> to stop."
while {[gets stdin] != "x"} {
   puts "lalal"
}

上面代码的问题是它将等待 stdin 而我不希望它等待。我希望代码始终被执行。

编辑 2011 年 9 月 8 日 - 上午 8.55

代码在名为 System Console (Altera) 的 FPGA 工具中使用。这确实适用于 TCL 命令,但不幸的是我不知道它可以处理哪些命令,不能处理哪些命令。

I would like to run a while loop until the stdin is filled with a character.

puts "Press x + <enter> to stop."
while {[gets stdin] != "x"} {
   puts "lalal"
}

The problem with the code above that it will wait for stdin and I don't want it to wait. I want the code to be executed all the time.

Edit 8th September 2011 - 8.55am

The code is used inside a FPGA tool called System Console (Altera). This does work with TCL Commands, but unfortunately I don't know which it can handle and which it doesn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-12-10 03:39:31

您应该在 stdin 上使用 fileevent 设置一个在通道变得可读后调用的函数,然后使用 vwait 运行事件循环。您可以使用后链来启动其他任务,以分段完成工作,而无需停止事件处理太长时间。

proc do_work {args} {...}
proc onRead {chan} {
    set data [read $chan]
    if {[eof $chan]} {
        fileevent $chan readable {}
        set ::forever eof
    }
    ... do something with the data ...
}
after idle [list do_work $arg1]
fconfigure stdin -blocking 0 -buffering line
fileevent stdin readable [list onRead stdin]
vwait forever

You should use a fileevent on stdin to set a function to be called once the channel becomes readable then use vwait to run the event loop. Your other tasks can be launched using after chains to have work done in pieces without halting the event processing for too long.

proc do_work {args} {...}
proc onRead {chan} {
    set data [read $chan]
    if {[eof $chan]} {
        fileevent $chan readable {}
        set ::forever eof
    }
    ... do something with the data ...
}
after idle [list do_work $arg1]
fconfigure stdin -blocking 0 -buffering line
fileevent stdin readable [list onRead stdin]
vwait forever
饭团 2024-12-10 03:39:31

如果将 stdin 通道置于非块模式,则 gets stdin 将返回空字符串(并且 fblocked stdin 将能够当输入不可用时返回 1),而不是等待某些事情发生。

# Enable magic mode!
fconfigure stdin -blocking 0

puts "Press x + <enter> to stop."
while {[gets stdin] != "x"} {
   puts "lalal"
   after 20;           # Slow the loop down!
}

# Set it back to normal
fconfigure stdin -blocking 1

事实上,你还可以使用系统stty程序来做甚至更奇特的事情

If you put the stdin channel into non-block mode, the gets stdin will return the empty string (and fblocked stdin will then be able to return 1) when input is not available, instead of waiting for something to happen.

# Enable magic mode!
fconfigure stdin -blocking 0

puts "Press x + <enter> to stop."
while {[gets stdin] != "x"} {
   puts "lalal"
   after 20;           # Slow the loop down!
}

# Set it back to normal
fconfigure stdin -blocking 1

In fact, you can also use the system stty program to do even more fancy things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文