繁体   English   中英

为什么在后台线程执行时此控件不更新其内容?

[英]Why doesn't this control update its contents while the background thread is executing?

taResults是一个TextArea控件。 该线程运行了两秒钟,但与此同时TextArea控件也从未更新过。 线程完成后,将更新taResults。

    private void ProcessSchema() {
             Task<Void> runnable = new Task<Void>() {
                  public Void call() {
                    try {Thread.sleep(2000);} catch (InterruptedException e) {}
                    return null;
                }
            };
            try {
                long startTime = System.currentTimeMillis();
                // This text never appears in the TextArea control
                taResults.setText("working...");
                Thread thread = new Thread(runnable);
                thread.start();
                while (true) {
                    if (thread.getState() == Thread.State.TERMINATED) {
                        break;
                    }
                }
                final long endTime = System.currentTimeMillis();
//              This text does appear in the control!
                taResults.setText("Total execution time: " + ((double)(endTime - startTime))/1000. + " seconds.");  
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
    }

您的忙碌循环正在FX Application Thread上执行,阻止了它执行其应做的工作(更新UI等)。 如果您想在Task完成后做一些事情,可以使用其onSucceeded处理程序:

private void ProcessSchema() {
    Task<Void> runnable = new Task<Void>() {
        public Void call() throws Exception {
            Thread.sleep(2000)
            return null;
        }
    };

    final long startTime = System.currentTimeMillis();

    runnable.setOnSucceeded(e -> {
        final long endTime = System.currentTimeMillis();
        taResults.setText("Total execution time: " + ((double)(endTime - startTime))/1000. + " seconds.");  
    });

    // if needed to can also do this, 
    // which gets executed if the task terminates with an exception:
    runnable.setOnFailed(e -> {
        runnable.getException().printStackTrace();
    });

    taResults.setText("working...");
    Thread thread = new Thread(runnable);
    thread.start();

}

这是一个完整的示例:

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TaskExample extends Application {

    private TextArea taResults ;
    private Button startButton;

    @Override
    public void start(Stage primaryStage) {
        taResults = new TextArea();
        taResults.setEditable(false);
        startButton = new Button("Start");
        startButton.setOnAction(e -> processSchema());

        BorderPane root = new BorderPane(taResults);
        root.setBottom(startButton);
        BorderPane.setAlignment(startButton, Pos.CENTER);
        BorderPane.setMargin(startButton, new Insets(5));

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private void processSchema() {
        Task<Void> runnable = new Task<Void>() {
            public Void call() throws Exception {
                Thread.sleep(2000);
                return null;
            }
        };

        final long startTime = System.currentTimeMillis();

        runnable.setOnSucceeded(e -> {
            final long endTime = System.currentTimeMillis();
            taResults.setText("Total execution time: " + ((double)(endTime - startTime))/1000. + " seconds.");  
            startButton.setDisable(false);
        });

        // if needed to can also do this, 
        // which gets executed if the task terminates with an exception:
        runnable.setOnFailed(e -> {
            runnable.getException().printStackTrace();
        });

        taResults.setText("working...");
        startButton.setDisable(true);
        Thread thread = new Thread(runnable);
        thread.start();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM