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.
my code:
in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
printToTerminal(line);
}
and the printToTerminal function:
protected void printToTerminal(String text) {
terminal.append(text + "\n");
terminal.redraw();
terminal.update();
}
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.
Is there a way to getting the input stream for my process and printing it to my GUI without stucking it?
Thanks for the help.
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 likeCompletableFuture
). If this is an Eclipse plug-in you can also use aJob
or a progress monitor).In your background thread you call
Display.asyncExec
to run the code which updates the UI (so just the call toprintToTerminal
here).Calling
Display.asyncExec
when code is already running in the UI thread does not work.