【发布时间】:2026-01-09 23:00:02
【问题描述】:
我有一个问题,但我无法找到解决方案。
我正在更新一个方法中的 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() 方法(扩展线程)。
【问题讨论】:
标签: java multithreading concurrency javafx