执行JavaMail时,所有JFrame都会冻结 [英] All JFrame freeze while do JavaMail

查看:104
本文介绍了执行JavaMail时,所有JFrame都会冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发汽车管理系统的程序。然后,当汽车进来和开车时,我想发送邮件给这家公司的老板。
我的代码可以成功发送邮件,但我注意到,当邮件发送完成时,其他JFrame窗口冻结(我无法在所有JFrame窗口上执行任何操作)。
这通常适用于Javamail还是有办法让其他JFrame仍在工作?

I develop program of car management system. Then I want to send mail to owner of this company when car coming in and car out. My code can send mail successfully but I notice that while mail send, other JFrame window is freeze (I can't do anything on all JFrame window) until mail send is complete. Is this normally for Javamail or there is a way to make other JFrame still working?

在我的程序中,完成发送一封邮件大约需要10秒钟。

In my program, it take about 10 seconds to complete send one mail.

推荐答案

当你执行繁重的任务时,你应该在另一个线程中运行它们,而不是像gui一样。如果您运行事件调度线程,那么gui将会冻结直到完成。

When you do heavy task you should run them in another threads rather in the same as gui. If you run in Event Dispatch Thread then the gui is gonna freeze until finish.

您可以使用 SwingWorker 这里是一个我非常喜欢的例子 Swing Worker示例

You can use SwingWorker here is an example i really like Swing Worker Example

示例:

class Worker extends SwingWorker<String, Object> {

    @Override
    protected String doInBackground() throws Exception {
       //here you send the mail
       return "DONE";
    }

    @Override
    protected void done() {
        super.done();
        //this is executed in the EDT
        JOptionPane.showMessageDialog(null, "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE);
    }
}

这篇关于执行JavaMail时,所有JFrame都会冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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