简体   繁体   English

main.js电子程序的执行顺序(其中还使用了nodejs的child_process的exec)

[英]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. 我注意到,当重新启动电子应用程序时,该应用程序的上一次启动仍保留了后台进程,这使后台进程随着该应用程序的重新启动次数的增加而堆积(如果该应用程序启动了10次,将会有10个应用程序的后台进程。)然后,当我进行下一次启动时,我决定终止上一次启动的后台进程。如下所示,我已经尝试进行相同的操作。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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