【发布时间】:2020-06-04 16:36:17
【问题描述】:
我正在尝试在 GLib.Subprocess 中运行可执行文件并提取其结果。但是,它可能是一个无限循环并且永远不会结束。所以,我希望子进程在 1 秒后结束。这是我尝试过的:
string executable = "/path/to/executable";
string input = "some_input";
uint timeout_id = 0;
...
...
try {
string output_string;
var subp = new GLib.Subprocess.newv ({executable}, SubprocessFlags.STDIN_PIPE | SubprocessFlags.STDOUT_PIPE);
timeout_id = GLib.Timeout.add (1, () => {
subp.force_exit ();
source_remove ();
return false;
});
subp.communicate_utf8 (input, null, out output_string, null);
} catch (GLib.Error e) {
print ("Error: %s\n", e.message);
}
...
...
void source_remove () {
if (timeout_id > 0) {
Source.remove (timeout_id);
timeout_id = 0;
}
}
我也尝试过使用{"timeout", "1", executable},但如果可执行文件是无限循环,它不会停止。
【问题讨论】: