In this Electron documentation page, they recommend that to restart an application, one should execute app.quit
or app.exit
after the call to app.relaunch
:
Note that this method does not quit the app when executed, you have to call app.quit or app.exit after calling app.relaunch to make the app restart.
However after experimenting I found that the order doesn't seem to actually matter. (See my example below.)
I know that app.quit
and app.exit
are not quite the same. The former can be interrupted and will trigger some events while the latter will force the app to exit without triggering any events or allowing the app to cancel the action.
Question: assuming that it is always ok to force the app to exit and that we don't have any tasks to perform before the app exits, is there:
app.quit
or app.exit
?app.quit
or app.exit
after app.relaunch
?Here's a very simple Electron app:
package.json
{
"name": "burrito",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^4"
}
}
main.js
const {app, BrowserWindow, Menu} = require('electron');
let mainWindow;
app.on('ready', () => {
Menu.setApplicationMenu(
Menu.buildFromTemplate([
{role: 'appMenu', submenu: [
{label: 'relaunch(); exit()', click() {
app.relaunch();
app.exit();
}},
{label: 'relaunch(); quit()', click() {
app.relaunch();
app.quit();
}},
{type: 'separator'},
{label: 'exit(); relaunch()', click() {
app.exit();
app.relaunch();
}},
{label: 'quit(); relaunch()', click() {
app.quit();
app.relaunch();
}}
]}
])
);
mainWindow = new BrowserWindow({width: 640, height: 480});
mainWindow.loadFile('index.html');
});
Producing the following application menu:
A click on any of the menu item will produce the same result: the app quits then restarts.
The proper way to restart an electron app is:
app.relaunch()
app.exit()
See official documentation.
app.relaunch
According to app.relaunch
docs:
Relaunches the app when current instance exits.
(...)
Note that this method does not quit the app when executed, you have to call
app.quit
orapp.exit
after callingapp.relaunch
to make the app restart.When
app.relaunch
is called for multiple times, multiple instances will be started after current instance exited.
We can assume that:
app.relaunch()
only won't do anything (until the user closes the application, then it will restart);app.quit()
or app.exit()
in order to close the application, then it will restart because you already called app.relaunch()
.app.quit
Citing the docs again:
Try to close all windows. The
before-quit
event will be emitted first. If all windows are successfully closed, thewill-quit
event will be emitted and by default the application will terminate.This method guarantees that all
beforeunload
andunload
event handlers are correctly executed. It is possible that a window cancels the quitting by returningfalse
in thebeforeunload
event handler.
It means that using app.quit()
may or may not terminate the application.
app.exit
The docs states:
All windows will be closed immediately without asking the user, and the
before-quit
andwill-quit
events will not be emitted.
So, immediately is a dangerous word. It means that the app will be closed as soon as it is possible, and that it certainly will close, but not instantly.
app.quit
or app.exit
?Both are valid, depends on your use case:
app.quit
is probably better for you. Just be careful to not call app.relaunch
more than once (it causes multiple instances to be started after exiting the current instance):app.relaunch();
app.quit(); // maybe the application will be closed; maybe not
app.exit
. Doing it you are safe that the app will be closed and relaunched:app.relaunch();
app.exit(); // the application will be closed as soon as possible
app.relaunch
first?Well, because it is logical. See the code below:
app.exit();
app.relaunch();
You're telling: "Hey, Electron, please close my app. Oh, and restart it after closing.". It'll work most of the times, but only because app.exit
is slower than app.relaunch
to execute. They're not running synchronously.
If, for some reason, Electron terminates your application before knowing it should be relaunched, it would be an unexpected behavior for you, but that's what you told Electron to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With