gpt4 book ai didi

java - 任务成功后进度指示器继续显示...我怎样才能停止它?

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

当单击按钮时,我启动一个任务来对用户进行身份验证,然后如果用户通过了身份验证,它将运行另一个任务来获取用户当前的开放类次。

我为此任务绑定(bind)了进度指示器...但问题是任务成功后指示器不会停止。

更新 1

我想做的是仅在用户经过身份验证时才运行 getUserShift 任务(身份验证返回的用户不为空)

那么这里的问题是什么:

@FXML
private void signInAction(ActionEvent event) {
String username = userNameText.getText();
String password = passwordText.getText();

Task<Void> runSignInTask = signInTask();
runSignInTask.setOnSucceeded(e -> {
System.out.println("SignIn Task Succeeded");
progressIndicator.progressProperty().unbind();
});
runSignInTask.setOnFailed(e -> {
System.out.println("SignIn Task Failed??!!");
});
progressIndicator.progressProperty().bind(runSignInTask.progressProperty());
new Thread(runSignInTask).start();
}

public Task<Void> signInTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
Task<User> runAuthTask = authenticate();
runAuthTask.setOnSucceeded(e -> {
System.out.println("Task Succeded!!");
//Check if user Authenticated
if (runAuthTask.getValue() != null) {
System.out.println("Access permitted");
//Run the second Task
Task<Shift> runGetUserShift = getUserShift(runAuthTask.getValue());
runGetUserShift.setOnSucceeded(l -> {
System.out.println("getUserShift task succeeded");
if (runGetUserShift.getValue() != null) {
Shift shift = runGetUserShift.getValue();
System.out.println("User has open shift");
}
});
runGetUserShift.setOnFailed(l -> {
System.out.println("getUserShift task Failed");
});
new Thread(runGetUserShift).start();
} else {
System.out.println("Access Denied!!");
}
});
runAuthTask.setOnFailed(e -> {
System.out.println("Task Failed!!");
});
new Thread(runAuthTask).start();
return null;
}
};
}

public Task<User> authenticate() {
return new Task<User>() {
@Override
protected User call() throws Exception {
return Authenticator.authenticated(userNameText.getText(), passwordText.getText());
}
};
}

public Task<Shift> getUserShift(User user) {
return new Task<Shift>() {
@Override
protected Shift call() throws Exception {
return ShiftDAO.getShift(user.getUserId(), user.getBranchId(), user.getComId());
}
};
}

最佳答案

这是一个应用程序设计错误,为什么您的 ProgressIndicator 在登录过程完成后没有停止。

我建议您使用单个任务来完成用户登录的所有工作。您的登录任务可能如下所示:

public Task<Void> signInTask() {
return new Task<Void>() {

@Override
protected Void call() throws Exception {

User user = Authenticator.authenticated(userNameText.getText(), passwordText.getText());
if(user != null){
Shift shift = ShiftDAO.getShift(user.getUserId(), user.getBranchId(), user.getComId());
if(shift != null){
// logic if user has a shift
} else {
// logic if user has no shift, throw exception, etc.
}
} else {
// logic if user is not authenticated, throw exception, etc.
}
return null;
}
}
}

另请参阅Concurrency in JavaFXJavaDoc - Task欲了解更多信息。

关于java - 任务成功后进度指示器继续显示...我怎样才能停止它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856098/

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