[英]JavaFX - How to pause a background Service with UI controller event?
對於JavaFX UI控制器事件,我想暫停后台服務,該服務以該UI控制器開頭(如以下代碼所示)。
我發現了類似的文章JavaFX2:我可以暫停后台任務/服務 。 但是,在發布該帖子時,它將提供解決方案來暫停具有自身事件的服務,而不是針對使用外部UI Controller觸發的事件。
實際上,在這種情況下,似乎必須設置服務的內部狀態以使其暫停,但輔助接口不包含這樣的狀態。 例如,我們可以通過服務的失敗狀態等強制服務失敗。
謝謝。
UI Controller類
public class CommandController implements Initializable {
private CommandService commandService;
private ExecutorService sequentialServiceExecutor;
private List<IOCommandService> servicePool = new ArrayList<>();
@Override
public void initialize(URL location, Resources resources) {
doProceedInitialSteps();
}
private void doProceedInitialSteps() {
// Select available IOCommands
List<IOCommand> commandList = commandService.getCommands();
// Initialised ExecutorService on sequential manner
sequentialServiceExecutor = Executors.newFixedThreadPool(
1,
new ServiceThreadFactory()
);
for (final IOCommand command : commandList) {
IOCommandService service = new IOCommandService(command, this);
service.setExecutor(sequentialServiceExecutor);
service.setOnRunning(new EventHandler<IOCommand>() { }
service.setOnSucceeded(new EventHandler<IOCommand>() { }
service.setOnFailed(new EventHandler<IOCommand>() { }
service.start();
servicePool.add(service);
}
}
@FXML
public void onCancel() {
// TODO: Need to pause current executing IOCommandService until receive the user response from the DialogBox
// Unless pause current executing IOCommandService, that will over lap this dialog box with IOCommandService related dialog boxes etc
Dialogs.DialogResponse response = Dialogs.showWarningDialog();
if(response.toString.equals("OK") {
}
}
}
服務等級
public class IOCommandService extends Service<IOCommand> {
private IOCommand command;
private CommandController controller;
public IOCommandService (IOCommand command, CommandController controller) {
this.command = command;
this.controller = controller;
}
@Override
protected Task<IOCommand> createTask() {
return new Task<IOCommand>() {
@Override
protected Integer call() throws Exception {
// Execute the IO command by,
// 1) updating some UI components (label, images etc) on CommandController
// 2) make enable popup some Dialog boxes for get user response
return command;
}
}
}
}
Hej Channa,
這是一個示例,您可以如何“暫停” Service
並在關閉Dialog
恢復它。
這是FXML;)
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="de.professional_webworkx.stopservice.FXMLController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
主要應用
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
Scene scene = new Scene(root);
stage.setTitle("JavaFX and Concurrency");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class FXMLController implements Initializable {
@FXML
private Label label;
private MyService ms;
@FXML
private void handleButtonAction(ActionEvent event) {
onCancel();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
ms = new MyService();
Platform.runLater(() -> {
ms.start();
});
}
public void onCancel() {
if(ms.getState().equals(Worker.State.RUNNING)) {
ms.cancel();
}
Scene s = new Scene(new Label("Dialog"), 640, 480);
Stage dialog = new Stage();
dialog.setScene(s);
dialog.showAndWait();
if(!dialog.isShowing()) {
ms.resume();
}
}
}
還有MyService類
我在控制台上運行for循環並打印出數字。 如果我調用MyService
的pause()
方法,我會cancel()
並在調用resume()
方法時restart()
。 Thread.sleep(2000L); 我只是添加了模擬長時間運行的Task
。
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
/**
*
* @author Patrick Ott
* @version 1.0
*/
public class MyService extends ScheduledService<Void> {
Task<Void> task;
int n = 1000000000;
@Override
protected Task<Void> createTask() {
return new Task() {
@Override
protected Object call() throws Exception {
for (int i = 0; i < n; i++) {
System.out.println("Halllo " + n);
Thread.sleep(2000);
n--;
}
return null;
}
};
}
public void pause() {
this.cancel();
}
public void resume() {
System.out.println("n="+n);
this.restart();
}
}
帕特里克
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.