JavaFX 中具有输入和输出参数的并发后台任务或服务
Concurrent background Task or Service with input and output arguments in JavaFX
它宁愿是一个非常简单的概念,但因为我对 JavaFX 中的并发完全陌生。我一直在努力理解这个概念。
在构建一个非常简单的 JavaFX 应用程序时,我想在后台执行一些冗长的任务并防止 UI 冻结。
在下面的示例中,我试图创建一个简单的后台任务服务,然后使用 ControlsFX 对话框在主界面上显示进度条 UI window。
但是,我想知道如何为该服务提供一些输入参数并从中获取布尔值输出。这在 C# BackgroundWorker 中非常简单,但在 JavaFX 中仍然无法弄清楚。任何提示都会有很大帮助。
试过这个简单的例子
public void runTask(Stage stage) throws URISyntaxException, IOException {
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws InterruptedException, URISyntaxException, IOException {
// some time consuming task here
// use the input arguments and perform some action on it
// then set the process result to a Boolean and return after the task is completed
// also keep hold back any other process to from executing on UI
return null;
}
};
}
};
Dialogs.create()
.owner(stage)
.title("Performing Task!")
.masthead("Please wait...")
.showWorkerProgress(service);
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
System.out.println("done:" + event.getSource().getValue());
}
});
service.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new UnsupportedOperationException("Failed."); //To change body of generated methods, choose Tools | Templates.
}
});
service.setOnCancelled(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new CancellationException("Cancelled."); //To change body of generated methods, choose Tools | Templates.
}
});
service.start();
}
对于输入参数,只需为方法定义一些参数。 (您需要 final
in Java 7。)
到return一个Boolean
,只需定义你的Service
和Task
的类型为Boolean
:
public void runTask(Stage stage, final int input /* can be any type */) throws URISyntaxException, IOException {
Service<Boolean> service = new Service<Boolean>() {
@Override
protected Task<Boolean> createTask() {
return new Task<Boolean>() {
@Override
protected Boolean call() throws InterruptedException, URISyntaxException, IOException {
// some time consuming task here
// use the input arguments and perform some action on it
// then set the process result to a Boolean and return after the task is completed
// also keep hold back any other process to from executing on UI
Boolean returnValue = ... ;
return returnValue;
}
};
}
};
Dialogs.create()
.owner(stage)
.title("Performing Task!")
.masthead("Please wait...")
.showWorkerProgress(service);
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
System.out.println("done:" + event.getSource().getValue());
}
});
service.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new UnsupportedOperationException("Failed."); //To change body of generated methods, choose Tools | Templates.
}
});
service.setOnCancelled(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new CancellationException("Cancelled."); //To change body of generated methods, choose Tools | Templates.
}
});
service.start();
}
它宁愿是一个非常简单的概念,但因为我对 JavaFX 中的并发完全陌生。我一直在努力理解这个概念。
在构建一个非常简单的 JavaFX 应用程序时,我想在后台执行一些冗长的任务并防止 UI 冻结。
在下面的示例中,我试图创建一个简单的后台任务服务,然后使用 ControlsFX 对话框在主界面上显示进度条 UI window。
但是,我想知道如何为该服务提供一些输入参数并从中获取布尔值输出。这在 C# BackgroundWorker 中非常简单,但在 JavaFX 中仍然无法弄清楚。任何提示都会有很大帮助。
试过这个简单的例子
public void runTask(Stage stage) throws URISyntaxException, IOException {
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws InterruptedException, URISyntaxException, IOException {
// some time consuming task here
// use the input arguments and perform some action on it
// then set the process result to a Boolean and return after the task is completed
// also keep hold back any other process to from executing on UI
return null;
}
};
}
};
Dialogs.create()
.owner(stage)
.title("Performing Task!")
.masthead("Please wait...")
.showWorkerProgress(service);
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
System.out.println("done:" + event.getSource().getValue());
}
});
service.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new UnsupportedOperationException("Failed."); //To change body of generated methods, choose Tools | Templates.
}
});
service.setOnCancelled(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new CancellationException("Cancelled."); //To change body of generated methods, choose Tools | Templates.
}
});
service.start();
}
对于输入参数,只需为方法定义一些参数。 (您需要 final
in Java 7。)
到return一个Boolean
,只需定义你的Service
和Task
的类型为Boolean
:
public void runTask(Stage stage, final int input /* can be any type */) throws URISyntaxException, IOException {
Service<Boolean> service = new Service<Boolean>() {
@Override
protected Task<Boolean> createTask() {
return new Task<Boolean>() {
@Override
protected Boolean call() throws InterruptedException, URISyntaxException, IOException {
// some time consuming task here
// use the input arguments and perform some action on it
// then set the process result to a Boolean and return after the task is completed
// also keep hold back any other process to from executing on UI
Boolean returnValue = ... ;
return returnValue;
}
};
}
};
Dialogs.create()
.owner(stage)
.title("Performing Task!")
.masthead("Please wait...")
.showWorkerProgress(service);
service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
System.out.println("done:" + event.getSource().getValue());
}
});
service.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new UnsupportedOperationException("Failed."); //To change body of generated methods, choose Tools | Templates.
}
});
service.setOnCancelled(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
throw new CancellationException("Cancelled."); //To change body of generated methods, choose Tools | Templates.
}
});
service.start();
}