Is it possible to quit the main application after launching an external file or app?
const child = require('child_process').execFile;
const fs = require('fs');
if (fs.existsSync(updateFile)) {
child(updateFile, function(err, data) { }); //start the update.exe
app.quit(); //quit the app
}
I'm trying to open my app updater.exe from the temp directory to install the new update. I cant use the autoUpdater for some reason.
The updater.exe is created using C#.net and it simply replaces the old files with the new ones after download, but I can't do that while the main application is still running. I'm thinking to kill the application from the C# .net through Process but it doesn't feel so right for me.
Using the code above in Electron-Node.js the updater.exe also quit after calling the app.quit() since it's just a child of the main application. What is the alternative method?
PS: This only supports Windows. Windows 10 and 11 to be more specific.
Thanks to @AlexanderLeither, it turns out that an option detached is available in child_process.spawn().
This makes it possible for the child process to continue running after the parent exits.
const child = require('child_process').spawn;
const subprocess = child(updateFile, {
detached: true, //Continue running after the parent exits.
stdio: 'ignore'
});
subprocess.unref(); //To prevent the parent from waiting for a given subprocess to exit
app.quit();
Also, for some reason, there are four electron processes when I tried to look in Taskmanager. I had to kill it using Process in my C# .net to avoid IO errors when replacing files.
while(true) { //don't stop killing untill no more process with the same name.
try {
Process[] process = Process.GetProcessesByName("TestProject"); //TestProject is the name of my current project and is in the TaskManager
if(process.Length < 1) break; //if the process doesn't exist then break this loop.
process[0].Kill(); //if the process exist, kill it
process[0].WaitForExit(); //wait until the process exits
} catch(Exception) {
//this will throw an exception if the same process is trying to kill while it is being kill, that's okay.
}
}