2

我正在使用设置为 -1.0 的指示器进度来显示loginprocess运行时的一些加载。但是当我按下 Enter 按钮并使用 启动执行程序时loginProcessfreezed即使我使用Plataform.runLater设置可见我的ProgressIndicator.

我的按钮事件:

public void initManager(final LoginManager loginManager) {
    btnEntrar.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {


            String email = loginTxtField.getText().trim();
            String token = tokenTxtField.getText().trim();

            if (email.equals("")) {
                Platform.runLater(new Runnable() {
                    public void run() {
                        Dialog.showError("Erro", "Digite o e-mail");
                    }
                });
                return;
            }

            try {

                Future future = loginProcess(email, token);

                showLoginLoading(future);

                future.get();

                if (!loginGatewayFailed && !loginTargetAppFailed) {

                    Login loginTargetApp = new Login(email, null, null);
                    loginManager.autheticated(loginTargetApp, loginGateway, gateway, file);

                } else {

                    if (loginTargetAppFailed) {

                        Platform.runLater(new Runnable() {
                            public void run() {
                                Dialog.showError("Erro", loginTargetAppFailedCause);
                            }
                        });

                    } else {

                        if (loginGatewayFailed) {

                            Platform.runLater(new Runnable() {
                                public void run() {
                                    Dialog.showError("Erro", loginGatewayFailedCause);
                                }
                            });
                        }
                    }
                }

            } catch (final Exception ex) {

                Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, ex.getMessage());

                Platform.runLater(new Runnable() {
                    public void run() {
                        Dialog.showError("Erro", ex.getMessage());
                    }
                });
            }
        }
    });
}

我的登录过程:

public Future<?> loginProcess(String email, String token) throws Exception {

// MY PROCESS

            return Executors.newSingleThreadExecutor().submit(new LoginTask(this, email, token));

        } catch (Exception e) {

            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, e.getMessage());

            throw e;
        }
    }

方法 showLoginLoading:

private void showLoginLoading(Future future) {
        while (!future.isDone()) {

            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    progressInd.setVisible(true);
                   // progressInd.setProgress(-1.0);
                }
            });

        }
    }
4

1 回答 1

0

问题出在线程管理中。我试图在主 FX 视图运行的同一线程中执行登录指令。我使用 Platform.isFxApplicationThread(); 如果调用线程是 JavaFX 应用程序线程,则返回 true。

为了解决我的问题,我只需要创建一个新线程来运行我的所有登录指令,如下例所示:

public void initManager(final LoginManager loginManager) {
    btnEntrar.setOnAction(new EventHandler<ActionEvent>() {
    boolean mainThread = Platform.isFxApplicationThread();
            System.out.println("This is the main Thread: " + mainThread);

            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    progressInd.setVisible(true);
                }
            });

            new Thread() {
                public void run() {

                    boolean mainThread = Platform.isFxApplicationThread();
                    System.out.println("This is the main Thread: " + mainThread);

                    String email = loginTxtField.getText().trim();
                    String token = tokenTxtField.getText().trim();

                    if (email.equals("")) {
                        Platform.runLater(new Runnable() {
                            public void run() {
                                Dialog.showError("Erro", "Digite o e-mail");
                            }
                        });
                        return;
                    }

                    try {

                        Future future = loginProcess(email, token);

            //            showLoginLoading(future);

                        future.get();

                        if (!loginGatewayFailed && !loginTargetAppFailed) {

                            Login loginTargetApp = new Login(email, null, null);
                            loginManager.autheticated(loginTargetApp, loginGateway, gateway, file);

                        } else {

                            if (loginTargetAppFailed) {

                                Platform.runLater(new Runnable() {
                                    public void run() {
                                        Dialog.showError("Erro", loginTargetAppFailedCause);
                                    }
                                });

                            } else {

                                if (loginGatewayFailed) {

                                    Platform.runLater(new Runnable() {
                                        public void run() {
                                            Dialog.showError("Erro", loginGatewayFailedCause);
                                        }
                                    });
                                }
                            }
                        }

                    } catch (final Exception ex) {

                        Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, ex.getMessage());

                        Platform.runLater(new Runnable() {
                            public void run() {
                                Dialog.showError("Erro", ex.getMessage());
                            }
                        });
                    }


                }
            }.start();
           });
        }
于 2013-02-14T19:31:49.540 回答