Posted to tcl by schelte at Sat Oct 05 13:44:24 GMT 2024view pretty

proc corobgexec {args} {
    set fd [open "|$args &"]
    fconfigure $fd -blocking 0
    fileevent $fd readable [list [info coroutine] data]
    set output {}
    while {![eof $fd]} {
        yield
        # Collect any output
        append output [read $fd]
    }
    fconfigure $fd -blocking 1
    try {
        close $fd
    } on ok {output} {
        puts "$cmd: Command successful"
    } trap {CHILDSTATUS} {output options} {
        set result [lindex [dict get $options -errorcode] end]
        if {$result == 3} {
            puts "$cmd: Argument undefined"
        } elseif {$result == 4} {
            puts "$cmd: Login Failed"
        } elseif {$result == 1} {
            puts "$cmd: Process Cancelled by user"
        }
    }
    return $output
}

proc steps {} {
    set result [corobgexec step1.sh]
    # update GUI
    set result [corobgexec step2.sh]
    # update GUI
    set result [corobgexec step3.sh]
    # update GUI
    set result [corobgexec step4.sh]
    # update GUI
    set result [corobgexec step5.sh]
    # update GUI
}

button .b1 -text Run -command [list coroutine bgsteps steps]

Comments

Posted by schelte at Sat Oct 05 13:46:54 GMT 2024 [text] [code]

$cmd should be $args.