另一个Thread正在运行时,JavaFX UI被阻止 [英] JavaFX UI blocked while another Thread is running

查看:176
本文介绍了另一个Thread正在运行时,JavaFX UI被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,而我却无法找到解决方案。

I have a problem, and I'm just not able to find a solution to it.

我正在更新方法中的UI,之后就是我正在开始发送电子邮件的主题。遗憾的是,在电子邮件线程完成或崩溃(异常)之前,UI不会更新。我已经尝试了所有可能的解决方案,我想到了(使用Platform.runLater()执行没有并行性,后台任务的所有内容...)。
下面我列出了项目的一些摘录。谢谢你的帮助!

I'm updating the UI in a method and right after that I'm starting a thread sending an e-mail. Sadly the UI won't update until the e-mail thread has finished or crashed (exception). I've tried every possible solution which came to my mind (do everything without parallelism, background tasks with Platform.runLater() ...). Below I have listed some extracts from the project. Thank you already for helping!

@FXML
public void sendMail() {
    // shows a new dialog (while the mailer is sending)
    main.showWaitingFrame();
    String[] filePaths = new String[Main.tempFilePaths.size()];
    for (int i = 0; i < filePaths.length; i++)
        filePaths[i] = Main.tempFilePaths.poll();
    Thread mailer = new Mailer(filePaths, getMailText(), this);
    mailer.setDaemon(false);
    mailer.start();
    try { mailer.join(); } catch (InterruptedException e) { e.printStackTrace(); }
    resetUI();
}

sendMail()方法。

public void showWaitingFrame() {
    try {
        FXMLLoader loader = new FXMLLoader(Main.class.getResource("ui/WaitingFrame.fxml"));
        waitingFrame = loader.load();
        waitingFrameController = loader.getController();
        waitingFrameController.setMain(this);
        waitingFrameController.setMainFrameController(mainFrameController);
        waitingFrameController.setWaitingFrameState(WaitingFrameState.SENDING);
        Stage stage = new Stage();
        stage.setScene(new Scene(waitingFrame));
        stage.show();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

showMail调用showWaitingFrame()方法( )方法并显示一个新对话框,通知用户该邮件当前正在发送。

The showWaitingFrame() Method is called by the sendMail() Method and displays a new dialog where the user is informed that the mail is currently sending.

@Override
public void run() {

    Thread.yield(); 

    String to = Accounts.MASTER.getMail();

    String from = Settings.userAccount.getMail();
    final String username = Settings.userAccount.getUsername();
    final String password = Settings.userAccount.getPassword();

    String host = "smtp.gmail.com";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
               return new javax.mail.PasswordAuthentication(username, password);
            }
    });

    try {

        BodyPart placeholder = new MimeBodyPart();
        placeholder.setText("\n" + "\n");

        BodyPart bodyText = new MimeBodyPart();
        BodyPart attachment = new MimeBodyPart();
        Multipart multipart = new MimeMultipart();

        bodyText.setText(bodyMessage + "\n" + "\n" + "\n");
        multipart.addBodyPart(bodyText);

        for (String s : filePaths) {
            DataSource source = new FileDataSource(s);
            attachment.setDataHandler(new DataHandler(source));
            attachment.setFileName(Time.shortToLongVersion(s.substring(s.length() - 36, s.length() - 17)) + " - " + "Screenshot");
            multipart.addBodyPart(attachment);
            multipart.addBodyPart(placeholder);
        }

        String subject = filePaths.length == 1 ? "Screenshot von " : "Screenshots von ";
        subject += Settings.userAccount.getName() + " | " + Time.getCurrentTimeString(true);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        message.setContent(multipart);

        long START = System.currentTimeMillis();
        Transport.send(message); // To-Do: Catch MailConnectException (and UnknownHostException)
        long END = System.currentTimeMillis();
        long INTERVAL = (END - START) / 1000;
        AppLogger.getInstance().log("Sent mail successfully (" + INTERVAL + " sec).");

        Platform.runLater(new Runnable() {
            @Override public void run() {
                main.getWaitingFrameController().setWaitingFrameState(WaitingFrameState.SUCCESS);
            }
        });

        FileHandler.deleteDirectory(new File(Main.settings.getProperty("filePathTempShots")));
        Main.tempFilePaths.clear();

    } catch (MessagingException me) {
        me.printStackTrace();
        AppLogger.getInstance().log("Sending mail failed. \n" + me.getMessage());
        for (String s : filePaths)
            Main.tempFilePaths.add(s);
    }

}

运行() Mailer的方法(扩展线程)。

Run() method of the Mailer (extends Thread).

推荐答案

sendMail()您正在启动一个新线程,并通过调用 mailer.join()等待其完成。这意味着JavaFx线程仍被 join 调用阻止。

In sendMail() you are starting a new thread and wait on its completion by calling mailer.join(). That means the JavaFx thread remains blocked by the join call.

请参阅: https://docs.oracle.com/javase/7/docs/api /java/lang/Thread.html#join()

一个简单的解决方案是将完成监听器添加到您调用的自定义Mailer类中 run()方法已完成:

A simple solution would be to add a completion listener to your custom Mailer class that you call after the run() method has finished:

   public void run() {
      try {
         ...
         listener.onSuccess();
      } catch (Exception e) { 
          listener.onError(); 
      }
   }

在JavaFX类中注册监听器

In your JavaFX class register the listener

mailer.addCompletionListener(new Listener {
    public void onSuccess() {
       Platform.runLater(new Runnable() {
           public void run() {
                resetUI();
           }
       });
    }
});

确保在JavaFX线程中执行与UI相关的操作。这可以通过使用 Platform.runLater 来完成( https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang .Runnable-

Make sure to execute UI related operations in the JavaFX thread. This can be done by using Platform.runLater (https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-)

示例代码只是为了解释如何解决它,它绝不是一个漂亮的解决方案;)

The sample code is just to explain how to solve it, its by no means a beautiful solution ;)

这篇关于另一个Thread正在运行时,JavaFX UI被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆