gpt4 book ai didi

另一个线程正在运行时,JavaFX UI 被阻塞

转载 作者:行者123 更新时间:2023-11-30 11:04:55 25 4
gpt4 key购买 nike

我有一个问题,但我找不到解决方案。

我正在更新一个方法中的 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);
}

}
Mailer 的

Run() 方法(扩展 Thread)。

最佳答案

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- )

来完成

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

关于另一个线程正在运行时,JavaFX UI 被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com