1

我们有一个 SWT 应用程序,当它仍在运行时会导致挂起,并且用户在 OS X 上触发系统关闭。从应用程序菜单关闭应用程序可以正常工作。

我试图为实例注册一个SWT.Close监听器:Display

display.addListener(SWT.Close, new Listener() {
    @Override
    public void handleEvent(Event event) {
        if (!handleExitRequest()) {
            event.doit = false;
            event.type = SWT.None;
        }
    }
});

这神奇地解决了挂起,但不幸的是,退出时调用它是不可靠的。:(

4

2 回答 2

1

I have had mixed results listening for shutdown hooks on the main thread. Sometimes it works and sometimes it doesn't. What has worked for me in the past is to create a separate thread and register the shutdown hook to that.

public class MyGuiApplication
{
    public static void main( String[] args )
    {
        Runtime runtime = Runtime.getRuntime();
        Thread shutdownThread = new Thread(new Runnable()
        {
          @Override
          public void run()
          {
            // Put graceful shutdown code of the main application/thread here.
          }
        });

        runtime.addShutdownHook( shutdownThread );

        startMyApp();
    }
}

Hopefully that might work for you.

于 2013-02-05T15:35:08.903 回答
0

看看这堂课和这堂课

Application 类允许您将 Java 应用程序与本机 OS X 环境集成。...如果用户在您的应用程序中有未保存的更改,请取消关闭/注销。

你也可以看看以下问题:

当应用程序是代理时,如何在 Mac OS X 中取消关机?

于 2013-02-05T21:25:37.003 回答