Reputation: 23
I'm trying to have my electron app do a bunch of cleanup when the app is quit (terminate a few processes, delete some temp files, etc). I am triggering the cleanUp function with the before-quit event. If my computer is running fast, these cleanup operations complete before the app is quit, but if the computer is running slow, the cleanUp function sometimes only executes partially.
Is there a way that I can prevent the app from fully quitting before my cleanUp function has been fully executed?
app.on('before-quit', async () => {
try {
await cleanUp();
} catch (err) {
console.error(err);
}
});
Upvotes: 2
Views: 3452
Reputation: 3527
As you can read in the before-quit
docs, you can use event.preventDefault()
inside the before-quit
event handler to prevent the app from terminating.
Your cleanup code can then run unimpeded. At the end of the cleanup, close the app programmatically.
To make sure that before-quit
will not block app termination at that time, you probably want to keep track of the current state of the app. You may want to allow the app to terminate inside before-quit
if the cleanup code has completed. That means: execute event.preventDefault()
only if the cleanup has not been completed yet.
It's probably wise to inform the user about the state of the app: display "shutting down" or something similar, so that it is clear that the app is no longer functional.
Upvotes: 3