【问题标题】:How to quit application using command line arguments inside a Task如何在任务中使用命令行参数退出应用程序
【发布时间】:2019-09-23 16:54:25
【问题描述】:

我正在使用 electron 5.0.0,我正在尝试使用 windows JumpList 和 Task 类别来退出我的电子应用程序。

    {
        program: process.execPath,
        arguments: '--new-window',
        iconPath: process.execPath,
        iconIndex: 0,
        title: 'New Window',
        description: 'Create a new window'
    }
])

我正在尝试修改电子网站上的示例代码,我需要更改参数

"arguments String - 程序执行时的命令行参数。"

我知道 windows 已经内置了 --new-window 之类的参数

所以我的问题是 windows 是否有一些东西会退出应用程序,或者我是否需要自定义参数,如果是这样,我将如何去做

我希望它具有与 Skype 相同的功能参见图片

编辑:

我尝试使用第二实例事件,但当用户点击任务时似乎没有调用它

app.setUserTasks([
    {
        program: process.execPath,
        arguments: '--force-quit',
        iconPath: process.execPath,
        iconIndex: 0,
        title: 'Force Quit App',
        description: 'This will close the app instead of minimizing it.'
    }
])
app.on('second-instance', (e, argv)=>{
    console.log("secinst" + argv)
    if(argv === '--force-quit'){
        win.destroy();
    }

})

【问题讨论】:

  • 尝试运行--help而不是--new-window,看看是否有关闭窗口的命令,因为我个人不知道是否有关闭窗口的命令。让我知道之后会发生什么!
  • 什么也没发生我也尝试在命令提示符下运行它。我发现在 SetUserTask 中,Program arg 指的是将要启动的程序,所以基本上它所做的是它再次使用参数启动同一个程序。它的意思是 chrome 中的“新窗口”之类的东西,您可以在其中创建一个新窗口,但有人知道如何关闭当前应用程序。
  • 您应该使用second-instance 事件处理该命令。
  • @hijleh 你能详细说明一下吗?

标签: javascript electron


【解决方案1】:

如果你这样设置任务:

app.setUserTasks([
    {
        program: process.execPath,
        arguments: '--force-quit',
        iconPath: process.execPath,
        iconIndex: 0,
        title: 'Force Quit App',
        description: 'This will close the app instead of minimizing it.'
    }
])

单击时,这将使用命令行参数--force-quit 启动应用程序的新实例。你应该处理那个论点。

只有当您允许应用程序的single instance 运行时,您的用例才有意义。您需要从second-instance 事件中获取argv

const { app } = require('electron')
let myWindow = null

const gotTheLock = app.requestSingleInstanceLock()

if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, argv, workingDirectory) => {
    // Someone tried to run a second instance
    const forceQuit = argv.indexOf("--force-quit") > -1;
    if (forceQuit) app.quit()
  })

  // Create myWindow, load the rest of the app, etc...
  app.on('ready', () => {
  })
}

【讨论】:

  • 所以我尝试实现你所说的。 (我已经阻止了双实例)但是对于某些原因,第二个实例没有被调用,它打开了样板应用程序(i.gyazo.com/240c8a3e6b473a8fb246629787634036.png)并且第二个实例方法监听器永远不会被调用。我认为程序路径可能是错误的,但我不知道如何找到正确的路径。 @hijeh
  • 没关系,我想通了。 process.execpath 仅在正确构建应用程序时才起作用。我还必须使用 win.destroy() 因为退出只会最小化它,因为我有一些其他代码。
  • 我想你也可以使用 app.exit() 来强制关闭应用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 2017-04-15
  • 1970-01-01
  • 2012-08-30
  • 2021-11-26
  • 2012-03-16
相关资源
最近更新 更多