繁体   English   中英

Windows关闭时不同的shutdown-hook行为

[英]Different shutdown-hook behavior on Windows shutdown

在使用Java 7运行的Eclipse E4 RCP应用程序中,我遇到的问题是,当Windows(7)关闭或注销时,不会执行shutdown-hook。

经过一些研究后,我发现了以下Open JDK错误条目 ,它创建了一个空的Swing JFrame并使用了一个shutdown-hook。 关闭钩子创建一个文件并写入它。 虽然这个bug与我的问题没有直接关系,但我接受了代码并对其进行了一些修改(见下文)。 (最大的修改是它不会陷入困境。)

然后我从该代码创建了一个jar并用javaw -jar jarname.jar执行它。 然后我尝试注销并关闭,在这两种情况下都写入了预期的文件。

之后我修改了代码,以便创建一个空的SWT Shell并使用相同的shutdown-hook(另见下文)。 注销和关闭时,不会写入预期的文件。

现在我想知道为什么行为如此不同? 我发现特别难以找到有关Windows和关机钩子的最新信息。

此外,如何确保在我的RCP应用程序中执行shutdown-hook?

Swing代码

public class ShutdownHookSwingBug {

  public static final String fileName = "C:/shutdownhookbug_swing.log";

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    File myFile = new File(fileName);
    myFile.delete();

    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          // Write "Hello " to a file
          File myFile = new File(fileName);
          FileOutputStream outputStream = new FileOutputStream(myFile);
          outputStream.write("Hello ".getBytes());
          outputStream.flush();
          // Write "world!" and close the file
          outputStream.write("world!".getBytes());
          outputStream.flush();
          outputStream.close();
        } catch (Exception e) {
          // Shouldn't happen.
        }
      }
    });
  }
}

SWT代码

public class ShutdownHookSwtBug {

  public static final String fileName = "C:/shutdownhookbug_swt.log";

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(300, 300);

    File myFile = new File(fileName);
    myFile.delete();

    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          File myFile = new File(fileName);
          FileOutputStream outputStream = new FileOutputStream(myFile);
          outputStream.write("Hello ".getBytes());
          outputStream.flush();
          outputStream.write("world!".getBytes());
          outputStream.flush();
          outputStream.close();
        } catch (Exception e) {
          // Shouldn't happen.
        }
      }
    });

    shell.open();
    while(!shell.isDisposed()){
      if(!display.readAndDispatch()){
        display.sleep();
      }
    }
    display.dispose();
  }
}

作为一种解决方法,我在E4 RCP应用程序中使用以下代码:

  SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(false);
    }
  });

但我不满意,因为:

  • 我不知道为什么会这样
  • 我正在将Swing混合到纯SWT应用程序中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM