Detect end of TCL background process in a TCL script

后端 未结 2 2025
难免孤独
难免孤独 2021-01-01 06:43

I\'m working on a program the uses an EXEC command to run a make file. This can take a long time so I want to put it in the background so the GUI doesn\'t lock up. However I

2条回答
  •  天命终不由人
    2021-01-01 07:33

    You want to run the make process in a pipeline and use the event loop and fileevent to monitor its progress (see http://wiki.tcl.tk/880)

    proc handle_make_output {chan} {
        # The channel is readable; try to read it.
        set status [catch { gets $chan line } result]
        if { $status != 0 } {
            # Error on the channel
            puts "error reading $chan: $result"
            set ::DONE 2
        } elseif { $result >= 0 } {
            # Successfully read the channel
            puts "got: $line"
        } elseif { [chan eof $chan] } {
            # End of file on the channel
            puts "end of file"
            set ::DONE 1
        } elseif { [chan blocked $chan] } {
            # Read blocked.  Just return
        } else {
            # Something else
            puts "can't happen"
            set ::DONE 3
        }
    }
    
    set chan [open "|make" r]
    chan configure $chan -blocking false
    chan event $chan readable [list handle_make_output $chan]
    vwait ::DONE
    close $chan
    

    I'm not certain about the use of vwait within Tk's event loop. Perhaps an expert will help me out here.

提交回复
热议问题