Andy wrote:
> 在 2012年9月28日星期五UTC+8下午7时00分04秒,Uwe Klein写道:
>
>>Andy wrote: > Another question, how to get output message? I mean the message generated by ping command. #!/usr/bin/wish button .start -text Start -command {handlecmd X start} button .stop -text Stop -command {handlecmd X stop} grid .start .stop # create a loop filedescriptor, there are various other ways # to achieve the same: set ::pfd [open |cat RDWR ] fconfigure $::pfd -buffering none # create an event handler for (returning) output # and a proc to handle events: proc outputevent {fd} { puts stderr PFD:[ gets $fd ] } fileevent $::pfd readable [list outputevent $::pfd] proc handlecmd {_id action} { upvar ::$_id id switch -- $action \ start { if {![catch {set id} cerr]} { puts stderr "Task runs already" return } # redirect all output to a file desciptor: set id [ exec ping localhost >&@ $::pfd & ] } stop { if {[catch {set id} cerr]} { puts stderr "Task does not run" return } exec kill $id unset id } } uwe
>
>
> Hi Uwe
> I run this code in Windows 7, but got nothing. Is there something wrong?
>
There is no 'cat' on windows. ( obviously, it is an inferior platform ;-)
if you use tcl 8.6 onwards you can replace it with
earlier? use [pipe] from tclx package
foreach {::rpfd ::wpfd } [chan pipe] break
# set ::pfd [open |ping RDWR]
# fconfigure $::pfd -buffering none
>
> proc outputevent {fd} {
> set data [ read $fd nonewline ]
> puts $data
> }
>
fileevent $::rpfd readable [list outputevent $::rpfd]
>
>
> proc handlecmd {_id action} {
> upvar ::$_id id
> switch -- $action {
> start {
> if {![catch {set id} cerr]} {
> puts stderr "Task runs already"
> return
> }
set id [ exec ping localhost >&@ $::wpfd & ]
> }
> stop {
> if {[catch {set id} cerr]} {
> puts stderr "Task does not run"
> return
> }
> exec taskkill /F /PID $id
> unset id
> }
> }
> }
>
> handlecmd X start
uwe