Process output only becomes available after the process has finished

前端 未结 5 1297
死守一世寂寞
死守一世寂寞 2021-01-07 03:50

I have a Runnable that reads Console output from an externally called exe (see below) and writes it to both a log file and a JTextArea.

But my Runnable doesn\'t show

5条回答
  •  借酒劲吻你
    2021-01-07 04:16

    Maybe it's because you don't respect Swing's threading policy. All accesses to swing components must be done in the event dispatch thread. Your runnable should thus use SwingUtilities.invokeLater to update the text area in the EDT, rather than in your separate thread.

    EDIT : as alf mentions in his comment: JTextArea.append is thread-safe, so it's not absolutely needed here. I would still do it, though, because if the append to a text area was replaced or complemented by any other Swing interaction, it wouldn't be thread-safe anymore.

    It could also be that the external process doesn't send any newline character, which makes readLine block until one is found or the end of communication is reached.

提交回复
热议问题