【问题标题】:Why JavaFX application Task Manager process doesn't end when closed为什么 JavaFX 应用程序任务管理器进程在关闭时不会结束
【发布时间】:2018-01-24 08:02:30
【问题描述】:

由于某种原因,我的应用程序中的某些进程即使在关闭应用程序窗口并退出后仍会继续。有什么我没有考虑到的吗?我的应用程序会定期生成系统托盘通知,但这些通知即使在退出后也会继续。阻止它的唯一方法是在任务管理器中找到 java.exe 进程。有什么我忽略的吗?

public class TrayNotification {

    public static void execute(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
        if (SystemTray.isSupported()) {
            TrayNotification trayNotification = new TrayNotification();
            trayNotification.displayTray(firstName, incidentNumber, requestId, tabTable);
        }
    }

    private void displayTray(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
        // Obtain a single instance of SystemTray
        SystemTray tray = SystemTray.getSystemTray();

        // Set TrayIcon values
        Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/tray-icon.png"));
        final TrayIcon trayIcon = new TrayIcon(image, "Remedy React Notification");
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip(incidentNumber);

        // Add TrayIcon to SystemTray instance if possible
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            // System.out.println("Notification could not be added.");
        }

        trayIcon.addActionListener(e -> {
            Parent root = null;
            try {
                root = FXMLLoader.load(getClass().getResource("/fxml/scene.fxml"));
                root.requestLayout();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.out.println("Test");
        });

        // Display TrayIcon 
            trayIcon.displayMessage("Hey, " + firstName, "You are now tracking " + incidentNumber + " !", TrayIcon.MessageType.INFO);

    }

}

【问题讨论】:

  • 最可能的原因是创建了非守护线程。您是否在程序中显式地或通过执行程序创建任何新线程?考虑尝试提供minimal reproducible example
  • 更多herehere

标签: java javafx


【解决方案1】:

当用户点击“退出”按钮时,我使用的是System.exit(0)

【讨论】:

  • 我的问题是我没有指定窗口的“退出”按钮的处理程序。我没有退出菜单项,也不会实施......我想我需要一些类似这篇文章的内容:stackoverflow.com/questions/26619566/javafx-stage-close-handler
  • @santafebound - 如果您没有“退出”选项,您如何定义“退出”?您说“这些即使在退出后也会继续” - 在“退出”时,调用 System.exit(0) 应该终止 JVM 实例。
  • 您是否使用左上角/右上角的 [X] 退出应用程序?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多