将流程输入流数据写入GUI [英] Writing process input stream data to GUI

查看:147
本文介绍了将流程输入流数据写入GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的GUI的Java代码中使用SWT,并且正在执行一个用python编写其代码的过程. 我使用流程输入流来捕获流程上的打印并将它们立即写入我的GUI.

Im using SWT in my java code for my GUI and im executing a process that his code is written in python. Im using the process input stream in order to catch the prints on the process and writing them immediatly into my GUI.

我的代码:

    in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
        printToTerminal(line);
    }

和printToTerminal函数:

and the printToTerminal function:

protected void printToTerminal(String text) {

        terminal.append(text + "\n");
        terminal.redraw();
        terminal.update();
    }

终端是一个Text对象,我想附加文本,然后使用重绘和更新以便立即向用户显示该行. 我的问题是我的GUI被卡住了,因为while循环完成了工作并获得了线路,但是我似乎无法获得不卡住我的GUI来获得线路的解决方案. 我尝试使用display.async函数,并在其中包含while循环,但仍然无法正常工作,并且GUI卡住了.

The terminal is a Text object which i would like to append the text and then using redraw and update in order to show the line immediatly to the user. my problem is that my GUI is getting stucked because of that while loop which doing the job and getting the lines but i cant seem to get the solution for getting the lines without stucking my GUI. I tried using display.async function and include the while loop in there but it still didnt worked and my GUI got stucked.

有没有一种方法可以获取过程的输入流并将其输出到GUI而不卡住?

Is there a way to getting the input stream for my process and printing it to my GUI without stucking it?

感谢您的帮助.

推荐答案

如果在用户界面线程中运行长时间运行的代码,它将阻止UI.因此,您必须在后台运行此代码.您可以使用Thread(或类似CompletableFuture之类的东西).如果这是Eclipse插件,则还可以使用Job或进度监视器).

If you run long running code in the User Interface thread it will block the UI. So you must run this code in the background. You can use a Thread (or something like CompletableFuture). If this is an Eclipse plug-in you can also use a Job or a progress monitor).

在后台线程中,您调用Display.asyncExec来运行用于更新UI的代码(因此此处仅调用printToTerminal).

In your background thread you call Display.asyncExec to run the code which updates the UI (so just the call to printToTerminal here).

在UI线程中已经运行代码时调用Display.asyncExec不起作用.

Calling Display.asyncExec when code is already running in the UI thread does not work.

这篇关于将流程输入流数据写入GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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