I have a batch file which is supposed to be started from my JavaFX application.
I have tried different implementations:
Append the output of the program in a file, then after the process finishes, read the file, and append the content to my TextBox.
This is wrong for many reasons, and mainly, because the GUI hangs, until the program finishes.
So I looked into using threads, but when using while(process.isAlive())
in a thread, the same thing happens.
I tried to do it so while the process is running, add contents of the output to my textBox. It only gets added after the program finished, even if I do this in a separate thread.
I have also bee trying to use a Platform.runLater(new Runnable() ...
.
With this, the content gets updated periodically, but still in big chunks of data at a time, and also makes the GUI hang.
Then I've found that JavaFX is not thread-safe, and that there is a library:
javafx.concurrent.Task
There is an example for it here: https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm
But still, I'm not sure if I am getting closer to what I am looking for...
Basically, I need something like this, but which makes the GUI not hang:
Process p = Runtime.getRuntime().exec(executeString);
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while(p.isAlive())
{
String line;
try
{
if ( (line = br.readLine()) != null)
logArea.appendText(line + "\n");
} catch(Exception e)
{
}
}