简体   繁体   中英

Order of execution of a program in main.js electron (where exec of child_process of nodejs is also used)

noobie here.. In the course of making an electron app, I have used electron-process package to make electron run in the background(even when all windows are closed). I noticed that, when the electron app was relaunched, the background process of the previous launch of the app remained, and this made background processes pile up with increase in the number of relaunches of the app (If the app was launched 10 times, I would have 10 background processes of the app).. Then I decided to terminate the background process of the previous launch, when I made the next launch.. I have made an attempt to do the same, which is shown below. I noticed that, the app was launched and almost instantly, the app was terminated(which I figured it out to be, the process termination part of the code being exectued later than the part which created the window and started the app. Hence terminating the current process only, instead of the process of the previous launch of the app).. Please help me in making this code run sequentially.. All comments, suggestions, explainations, advices are warmly welcomed. Thanks in advance..

'use strict';

const electron = require('electron');
const main = require('electron-process').main;
const app = electron.app;  // Module to control application life.
const BrowserWindow = electron.BrowserWindow;  // Module to create native browser window.
const exec = require('child_process').exec;
const exec1 = require('child_process').exec;
const fs = require('fs'); 
let mainWindow = null;
let mainWindow1 = null;

app.on('ready', function() {

    var pidrun;
    var killtask;
    var read;


    if(process.platform == 'win32'){

        exec('tasklist /v /fo csv | findstr /i "electron.exe"', (error, stdout,                                   stderr) => {
            if (error){
                console.error(`exec error: ${error}`);
                return;
            } 

            pidrun = stdout[16]+stdout[17]+stdout[18]+stdout[19];

            killtask = "Taskkill /FI \"PID eq "+pidrun+"\" /F";   
            exec(killtask, (error, stdout, stderr) => {
                if (error) {
                    console.error('exec error: ${error}');
                    return;
                }

                if(stdout == "INFO: No tasks running with the specified criteria."){
                    return;
                }

            });  

        });

    }

    const backgroundhtml = 'file://' + __dirname + '/background.html';
    const backgroundProcessHandler = main.createBackgroundProcess(backgroundhtml);
    mainWindow = new BrowserWindow({width: 1280, height: 600});
    backgroundProcessHandler.addWindow(mainWindow);

    mainWindow.loadURL('file://'+__dirname+'/foreground2.html');
    mainWindow.loadURL('file://' + __dirname + '/foreground.html');
    mainWindow.webContents.openDevTools()
});

在我看来,您随时都希望只运行一个应用程序实例,Electron app.makeSingleInstance专门提供了app.makeSingleInstance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM