我遇到了问题,我无法找到解决方案。
我正在更新方法中的UI,然后我就开始发送电子邮件的线程了。遗憾的是,在电子邮件线程完成或崩溃(异常)之前,UI不会更新。我已经尝试了所有可能的解决方案,我想到了(使用Platform.runLater()执行没有并行性,后台任务的所有事情...)。 下面我列出了项目的一些摘录。谢谢你的帮助!
@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();
}
}
showWaitingFrame()方法由sendMail()方法调用,并显示一个新对话框,通知用户该邮件当前正在发送。
@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);
}
}
邮件程序的 Run()
方法(扩展线程)。
答案 0 :(得分:1)
在sendMail()
中,您正在开始一个新线程并通过调用mailer.join()
等待其完成。这意味着JavaFx线程仍被join
调用阻止。
请参阅:https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
一个简单的解决方案是将完成侦听器添加到您在run()
方法完成后调用的自定义Mailer类中:
public void run() {
try {
...
listener.onSuccess();
} catch (Exception e) {
listener.onError();
}
}
在JavaFX类中注册监听器
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-)
示例代码只是解释如何解决它,它绝不是一个漂亮的解决方案;)
答案 1 :(得分:0)
删除mailer.join()。它导致当前线程(Platform thread!)暂停执行,直到邮件程序线程结束。
在JavaFX中,在后台运行代码的常用方法是使用Task。
您可能需要阅读Concurrency in JavaFX。