[英]Long running and blocking javafx task progress bar update
我有一个可以长时间运行的任务,它正在阻塞。 我必须等待此任务完成才能在GUI上显示结果。
我知道这项任务需要多长时间,因此我想通过进度条通知用户,剩下多少时间。
互联网上的所有例子都是这样的:
Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 1000; iterations++) {
updateProgress(iterations, 1000);
// Now block the thread for a short time, but be sure
// to check the interrupted exception for cancellation!
try {
Thread.sleep(100);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
}
}
return iterations;
}
};
此代码在循环中阻塞,并使用updateProgress
方法更新它的进度。
我的情况看起来像:
Task<Integer> task = new Task<Integer>() {
@Override
protected Integer call() throws Exception {
try {
//This is my long running task.
Thread.sleep(10000);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
return null;
}
}
}
return iterations;
}
};
这在循环中不起作用,因此我无法使用updateProgress
更新进度。
对此的一个解决方案是创建另一个将运行我的阻塞任务的任务,并计算进度,但这看起来不太优雅。
有一个更好的方法吗?
我发现很难看到任务阻塞的用例,但你知道它需要多长时间(至少有一个用动画API实现起来不容易)。 但假设你有一个,我会这样做:
您的任务是阻止的,因此必须在其他地方执行对进度的更新。 基本上,您需要一个定期更新进度的后台线程。 每次将帧渲染到屏幕时,执行此操作的最佳时间是: AnimationTimer
类完全是为此设计的。 因此,您希望任务在开始运行时启动动画计时器,更新handle(...)
方法中的进度,并确保在任务停止运行时停止。 这看起来像:
public abstract class FixedDurationTask<V> extends Task<V> {
private final Duration anticipatedRuntime ;
private AnimationTimer timer ;
public FixedDurationTask(Duration anticipatedRuntime) {
this.anticipatedRuntime = anticipatedRuntime ;
}
public Duration getAnticipatedRuntime() {
return anticipatedRuntime ;
}
@Override
protected void running() {
timer = new AnimationTimer() {
long startTime = System.nanoTime() ;
long endTime = (long) (startTime + anticipatedRuntime.toMillis() * 1_000_000) ;
long duration = endTime - startTime ;
@Override
public void handle(long now) {
if (isRunning()) {
updateProgress(now - startTime, duration);
} else {
stop();
if (getState() == State.SUCCEEDED) {
updateProgress(duration, duration);
}
}
}
};
timer.start();
}
}
这是一个最小的测试(为方便起见,包括上面的类)。 在文本字段中键入以秒为单位的时间,然后按Enter键 。
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FixedDurationTaskTest extends Application {
@Override
public void start(Stage primaryStage) {
TextField durationField = new TextField();
durationField.setPromptText("Enter time in seconds");
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
double duration = Double.parseDouble(durationField.getText());
return new FixedDurationTask<Void>(Duration.seconds(duration)) {
@Override
public Void call() throws Exception {
Thread.sleep((long) getAnticipatedRuntime().toMillis());
return null ;
}
};
}
};
ProgressBar progress = new ProgressBar();
progress.progressProperty().bind(service.progressProperty());
durationField.disableProperty().bind(service.runningProperty());
durationField.setOnAction(e -> service.restart());
VBox root = new VBox(10, durationField, progress);
root.setPadding(new Insets(20));
root.setMinHeight(60);
root.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static abstract class FixedDurationTask<V> extends Task<V> {
private final Duration anticipatedRuntime ;
private AnimationTimer timer ;
public FixedDurationTask(Duration anticipatedRuntime) {
this.anticipatedRuntime = anticipatedRuntime ;
}
public Duration getAnticipatedRuntime() {
return anticipatedRuntime ;
}
@Override
protected void running() {
timer = new AnimationTimer() {
long startTime = System.nanoTime() ;
long endTime = (long) (startTime + anticipatedRuntime.toMillis() * 1_000_000) ;
long duration = endTime - startTime ;
@Override
public void handle(long now) {
if (isRunning()) {
updateProgress(now - startTime, duration);
} else {
stop();
if (getState() == State.SUCCEEDED) {
updateProgress(duration, duration);
}
}
}
};
timer.start();
}
}
public static void main(String[] args) {
launch(args);
}
}
对于这些要求,我建议你使用Infinite ProgressIndicator 。
它有助于向用户显示正在进行的某些后台进程,并且在完成后将删除ProgressIndicator。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.