JavaFX - 尝试使用 BufferedReader 时冻结 - 未关闭?

JavaFX - Freezing when attempting to use BufferedReader - Unclosed?

我已经尝试了几个小时试图解决这个问题,但它开始让我烦恼。

我有一个简单的 JavaFX 应用程序,当单击一个按钮时,它

执行Process p = pb.start()

命令运行s,可以在终端看到windows,但是GUI完全卡死了。

//A lot of unused imports I know...

public class GUI extends Application {

    GridPane grid;
    Scene scene; 
    Button btn1;
    Button btn2;
    TextArea text;
    Label lbl1;
    Label lbl2;

    @Override

    public void start(Stage primaryStage) throws IOException {

        primaryStage.setTitle("Flashing - ROMMING - Recovery - Rooting");
        grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setGridLinesVisible(true);
        scene = new Scene(grid,1000, 1000);
        text = new TextArea();
        text.setText("Commands will be Shown Here");

        btn1 = new Button("ADB List Devices");
        btn2 = new Button("ADB Reboot");
        btn1.setMinSize(50, 25);
        btn2.setMinSize(50, 25);
        btn1.addEventHandler(ActionEvent.ACTION, lsDevice);

        grid.add(btn1, 0, 1);
        grid.add(btn2, 0, 2);
        grid.add(text, 0, 3);

        primaryStage.setScene(scene);
        primaryStage.show();

    }
    //Logger

    //Event Handlers
    EventHandler<ActionEvent> lsDevice = new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            System.out.println("Handling event " + e.getEventType());
            ProcessBuilder pb = new ProcessBuilder().command("cmd.exe");

            File log = new File("log");
            String line = null;
            try {
                 Process p = pb.start();
                 BufferedReader stdInput = new BufferedReader(new
                         InputStreamReader(p.getInputStream()));
                    // read the output from the command
                    while((line=stdInput.readLine())!=null){
                           System.out.println(line);
                    }
                }
                catch (Exception e1) {
                    e1.printStackTrace();
            }
            e.consume();
        } 
    };
}

如有任何帮助,我们将不胜感激。

附带说明一下,我的场景中有一个 TextArea,我想在命令为 运行 时更新它。

例如,如果单击 ver 按钮,我希望在 TextArea 中更新它。我试过了 text.appendText(string).

有什么帮助吗?

谢谢!

问题

问题是因为在 JavaFX 应用程序线程而不是后台线程中处理长 运行 任务。

解决方案

将处理工作移动到新任务中,并在单击按钮时启动任务。

Task<String> task = new Task<String>() {
     @Override 
     protected Integer call() throws Exception {
          try {
             Process p = pb.start();
             BufferedReader stdInput = new BufferedReader(new
             InputStreamReader(p.getInputStream()));
             // read the output from the command
             while((line=stdInput.readLine())!=null){
                  System.out.println(line);
                  // To update the textarea
                  updateMessage(line);
             }
          }
          catch (Exception e1) {
             e1.printStackTrace();
          }
     }
};

处理程序内部:

Thread th = new Thread(task);
th.setDaemon(true);
th.start();

textarea,可以绑定为

textarea.textProperty().bind().(Bindings.concat(textArea.getText()).concat(task.messageProperty()));

有关详细信息,请查看 Concurrency in JavaFX

N.B. 即时编写代码。可能包含拼写错误。