gpt4 book ai didi

java - 使用 2 个按钮启动和停止 JavaFX 后台线程

转载 作者:行者123 更新时间:2023-12-02 11:25:14 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的用户界面来启动一个 Selenium 测试,该测试能够启动一个后台线程,该线程在按下“开始”按钮时启动浏览器,并在按下“停止”按钮时停止线程并关闭它。不幸的是,当我启动后单击“停止”时,它不起作用。如果我让它完成,我将无法重新启动线程。我将如何更新它,以便我可以让它提交一个可以通过停止按钮停止的新线程。

        package application;

import org.openqa.selenium.WebDriver;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;


public class Main extends Application {
Stage window;
GridPane grid;
public void start(Stage primaryStage) {

/*
* Set up the stage
*/
window = primaryStage;
window.setTitle("URL LOADER - V1");
grid = new GridPane();
grid.setPadding(new Insets(10,10,10,10));
grid.setVgap(8);
grid.setHgap(10);
window.setResizable(false);

/*
* URL input
*/
Label URLLabel = new Label("URL");
GridPane.setConstraints(URLLabel,0,0);
TextField URLTextField = new TextField();
URLTextField.setPromptText("https://www.google.com");
GridPane.setConstraints(URLTextField,1,0);

/*
* Create Buttons
*/
Button buttonStart = new Button("Create");
GridPane.setConstraints(buttonStart,1,6);
Button buttonStop = new Button("Stop");
GridPane.setConstraints(buttonStop,1,8);

grid.getChildren().addAll(URLLabel,URLTextField, buttonStart, buttonStop);

/*
* Create the scene
*/
Scene scene = new Scene(grid, 300, 300);
window.setScene(scene);
window.show();

Task<Void> task = new Task<Void>(){
@Override
protected Void call() {
new VisitPage().Start(this,URLTextField.getText());;
return null;
}
};

buttonStart.setOnAction(new EventHandler<ActionEvent>() {
/*
* Start Button Clicked
*/
public void handle(ActionEvent event) {

new Thread(task).start();

}

});

buttonStop.setOnAction(new EventHandler<ActionEvent>() {
/*
* Start Button Pressed
*/
public void handle(ActionEvent event) {
System.out.println("Stop Pressed");

}
});

}

public class VisitPage {
private String URL;
Browser BrowserFactory;
ThreadLocal<WebDriver> drivers;
WebDriver Browser;
public void Start(Task<Void> task, String URL) {
while (true) {
if (task.isCancelled())
{
System.out.println("Canceling...");
System.out.println("Stop Pressed");
Browser.close();
Browser.quit();
BrowserFactory.CloseDriver(drivers);
task.cancel();
}
else
{
/*
* Create Browser Factor to make ThreadLocal Browsers
*/
BrowserFactory = new Browser(1, 1);
drivers = BrowserFactory.SpawnBrowser();
/*
* Grab a Browser
*/
Browser = BrowserFactory.SpawnDriver(drivers);
/*
* Visit and scrape
*/
Browser.get(URL);
/*
* Wait 5 Seconds before closing
*/
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Browser.close();
Browser.quit();
BrowserFactory.CloseDriver(drivers);
}

}
}

}


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

最佳答案

根据documentation

As with FutureTask, a Task is a one-shot class and cannot be reused. See Service for a reusable Worker.

因此您必须为每次运行创建新任务。所以我在 Main 中添加了任务作为字段:

Stage window;
GridPane grid;
Task<Void> task;

然后在单击开始按钮时创建任务:

buttonStart.setOnAction(new EventHandler<ActionEvent>() {
/*
* Start Button Clicked
*/
@Override
public void handle(ActionEvent event) {
if(task != null) {
System.out.println("Task already running");
return;
}
task = new Task<Void>() {
@Override
protected Void call() {
new VisitPage().start(this, URLTextField.getText());
;
return null;
}
};
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
});

单击停止按钮时,您必须取消任务:

buttonStop.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(task == null) {
System.out.println("Task not running");
return;
}
System.out.println("Stop Pressed");
task.cancel();
task = null;
}
});

这不会执行任何操作,因为您有责任在取消任务时结束任务,并且您不会结束无限循环。因此,您的 VisitPage 应如下所示(我跳过了测试详细信息,因为我在类路径上没有它们):

public class VisitPage {
public void start(Task<Void> task, String URL) {
while (!task.isCancelled()) {
System.out.println("Running test");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Test run ended");
}
System.out.println("Canceling...");
System.out.println("Stop Pressed");
return;
}
}

一些小要点:

从技术上讲,如果您没有捕获线程 hibernate 时抛出的 InterruptedExceptiontask.cancel() 有时会结束您的线程。

我不确定你的代码是如何编译的,但我必须将一些变量设置为最终变量,以便它们可以在处理程序中使用:(没关系,从 Java SE 8 局部变量可以有效地为最终变量)

final TextField URLTextField = new TextField();
//...
final Task<Void> task = new Task<Void>(){
//...

我将创建的线程定义为守护进程,这样当您关闭 UI 而不停止测试时它就不会继续运行:

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

我还将 Start 方法重命名为 start

关于java - 使用 2 个按钮启动和停止 JavaFX 后台线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49685373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com