任务成功后进度指示器继续运行...我该如何阻止它?

时间:2017-12-17 14:30:46

标签: java javafx

单击按钮时,我启动任务以验证用户身份,然后如果用户通过身份验证,则会运行另一项任务以使用户获得当前的开放班次。

我将进度指示器绑定到此任务...但问题是指示在任务成功后不会停止。

更新1

我尝试做的是仅在用户通过身份验证时运行getUserShift Task(authenticate返回User not null)

那么这里有什么问题:

@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());
        }
    };
}

1 个答案:

答案 0 :(得分:1)

这是一个应用程序设计错误,为什么您的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以获取更多信息。