在TCL脚本中检测TCL后台进程结束 [英] Detect end of TCL background process in a TCL script

查看:282
本文介绍了在TCL脚本中检测TCL后台进程结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EXEC命令运行Make文件的程序.这可能需要很长时间,所以我想将其放在后台,这样GUI不会锁定.但是,我也希望禁用GUI并仅在编译make文件时运行进度条.

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 also want the GUI to be disabled and a progress bar to run only while the make file is compiling.

那么我该如何检测TCL的后台进度何时完成?

So how can I detect when a background progress has finished in TCL?

它变得更加复杂,因为我的老板希望命令窗口保持打开状态(或可见),以便用户可以查看制作进度并查看是否出错.

It gets more complicated because my boss wants the command window to stay open (or be visable) so the user can see the progress of the make and see if it errors.

P.S.弄清楚线程会更容易吗?我需要一些方法来防止GUI锁定(防止不响应).'

P.S. Would figuring out threading be easier? I need some way to prevent the GUI from locking up (prevent NOT RESPONDING).'

GUI是用TK制成的. 我认为TK是单线程的,这会导致问题.或者它可能默认为单线程,而我想将其设置为多线程.

The GUI is made with TK. I think TK is single-threaded which causes the problem. Or it could be that it defaults to single threaded and I want to set it to multi-thread.

推荐答案

正如@ glenn-jackman指出的那样,首选使用fileevent(因为它应可在任何地方使用).

As @glenn-jackman pointed out, the use of fileevent is preferred (because it should work everywhere).

proc handle_bgexec {callback chan} {
    append ::bgexec_data($chan) [read $chan]
    if {[eof $chan]} {
        # end of file, call the callback
        {*}$callback $::bgexec_data($chan)
        unset ::bgexec_data($chan)
    }
}

proc bgexec {callback args} {
    set chan [open "| $args" r]
    fconfigure $chan -blocking false
    fileevent $chan readable [list handle_bgexec $callback $chan]
    return
}

将其调用为bgexec job_done cmd /c start /wait cmd /c make all-all.完成命令后,job_done被命令的输出调用.

Invoke this as bgexec job_done cmd /c start /wait cmd /c make all-all. job_done gets called with the output of the command after it finishes.

也可以使用线程来执行此操作,但这需要线程化的tcl构建(现在所有平台AFAIK都默认使用该线程,但是unix下的Tcl esp.的较早版本默认情况下不构建线程化的Tcl. )和Thread软件包(默认包含).与线程一起使用的方法是:

It is also possible to use threads for this things, but this requires a threaded tcl build (which is now default for all platforms AFAIK, but older versions of Tcl esp. under unix don't build a threaded Tcl by default.) and the Thread package (which is included by default). An approach to use it with Threads would be:

thread::create "[list exec cmd /c start /wait cmd /c make all-all];[list thread::send [thread::id] {callback code}];thread::exit"

如果您需要定期调用此线程,则可能只使用一个工作线程,而不是为每个作业创建一个新的线程.

If you need to call this on a regular basis it might be worth to use only one worker thread instead of creating a new one for each job.

添加/wait作为参数以启动保持第一个cmd运行.

Add /wait as parameter for start the keep the first cmd running.

cmd /c start /wait cmd /c make all-all

这篇关于在TCL脚本中检测TCL后台进程结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆