在电子应用程序中调用app.quit()在Windows中不起作用(但在Linux上有效)

时间:2019-09-24 23:10:31

标签: electron

我有一个电子应用程序,它主要在后台运行,响应NATS客户端收到的消息,偶尔在屏幕的右下角打开一个窗口。

该应用程序具有任务栏图标和带有退出按钮的菜单,该退出按钮会调用electron.app.quit()

const createTray = () => {
  const trayIconPath = path.join(__dirname, "icon.png");
  tray = new Tray(trayIconPath);
  const menuTemplate = [
    {
      label: `Version: ${app.getVersion()}`,
      type: "normal",
      enabled: false
    },
    ...(other buttons here)...
    {
      label: "Quit",
      type: "normal",
      click: app.quit
    },
    { label: "Exit", type: "normal", click: app.exit }
  ];

  const contextMenu = Menu.buildFromTemplate(menuTemplate);
  tray.setToolTip("Tooltip text");
  tray.setContextMenu(contextMenu);
};

在linux上,此按钮可以正常工作。该应用程序退出,并且当我检查ps时,我发现该任务不再运行。

在Windows上,它似乎没有任何作用。该应用程序将继续运行,图标仍保留在任务栏中,如果我单击任务栏中的其他任何按钮,则它们的行为就像从未关闭过该应用程序一样。

请注意:退出按钮在两种平台上均可使用,但不会触发will-quit事件,我想向该事件添加处理程序。

为尝试调试,我尝试将处理程序添加到before-quitwill-quitquit事件中:

app.on("before-quit", () => {
  console.log("before-quit");
  dialog.showMessageBoxSync(null, {
    title: "Before Quit",
    message: "Before Quit",
    buttons: ["OK"]
  });
});

app.on("will-quit", () => {
  console.log("will-quit");
  dialog.showMessageBoxSync(null, {
    title: "Will Quit",
    message: "Will Quit",
    buttons: ["OK"]
  });
});

app.on("quit", () => {
  console.log("did quit");
  dialog.showMessageBoxSync(null, {
    title: "Did Quit",
    message: "Did Quit",
    buttons: ["OK"]
  });
});

在Linux上,按“退出”后,我看到在退出应用程序之前打开了所有三个对话框。

在Windows上,按“退出”后,我看到Before Quit对话框打开,但其他两个都没有。

目前这些事件没有其他处理程序。

我尝试通过以下方法替换“退出”按钮的点击处理程序,从而在对app.quit的调用周围添加了一个try / catch块:

const quit = () => {
  try {
    mainWindow.close();
    app.quit();
  } catch (err) {
    console.error(err);
    dialog.showMessageBoxSync(null, {
      title: "Error quitting",
      message: err.message,
      buttons: ["OK"]
    });
  }
};

进行此更改并尝试在Windows上退出应用程序后,我仍然看到Before Quit对话框,但看不到任何错误对话框,并且该应用程序继续运行。

那么,是什么导致我的应用无法在Windows中正常退出?我还能做些什么来调试问题?

1 个答案:

答案 0 :(得分:0)

从电子6.0.1升级到6.0.10似乎已经解决了这个问题