最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you force an electron app to have a single instance? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to keep my electron app from being able to start more than one window at a time. I've already tried this, but it makes the first window opened close itself instead of the second window opened, and it pletely stops working for the third window:

app.requestSingleInstanceLock();
app.on('second-instance', () => {
    app.quit();
});

The older app.makeSingleInstance also throws an error since it's deprecated. What should I do instead?

I'm trying to keep my electron app from being able to start more than one window at a time. I've already tried this, but it makes the first window opened close itself instead of the second window opened, and it pletely stops working for the third window:

app.requestSingleInstanceLock();
app.on('second-instance', () => {
    app.quit();
});

The older app.makeSingleInstance also throws an error since it's deprecated. What should I do instead?

Share Improve this question asked Jan 28, 2020 at 4:19 Greyson R.Greyson R. 4606 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Update: the example in the docs looks like the best pattern to follow! I.e. call app.quit() when app.requestSingleInstanceLock() returns false


From the docs:

This event will be emitted inside the primary instance of your application when a second instance has been executed and calls app.requestSingleInstanceLock().

I.e. this is why app.quit() shuts the first window.

... Usually applications respond to this by making their primary window focused and non-minimized.

So if win is the instance of BrowserWindow that your main process opened, you could do:

win.show()
win.focus()

I believe you could also do nothing in the 'second-instance' handler: the event is just for information, telling you that the user has tried to open your app a second time.

发布评论

评论列表(0)

  1. 暂无评论