I have to press ctrl+c every time i exit


#1

hi all

i have to press ctrl+c every time i press x or exit the app , how can i solve this ??

FYI here is my main code :

app.on('window-all-closed', function() {
    if (process.platform != 'darwin') {
        app.quit();
    }

});

app.on('ready', function() {
    const modalPath = path.join('file://' + __dirname + '/app/index.html');
    mainWindow = new BrowserWindow({width: 1280, height: 700,minWidth:1280, minHeight: 700 ,maximizable : true});
    mainWindow.loadURL(modalPath);
    mainWindow.on('close', function () { mainWindow = null; });
    mainWindow.on('closed', function() { mainWindow = null; });

    var newProject = new BrowserWindow({width: 1000, height: 700 ,minWidth: 1000, minHeight: 700 ,maximizable : true,show: false})
    newProject.loadURL('file://'+ __dirname +'/project/index.html');


    ipc.on('show-project-win', function(){
       newProject.show();
    });

    newProject.on('close', function (event) {
        newProject.hide();
        event.preventDefault();
    });


});

#2

Have you tried following the pattern described in the Electron Quick Start Guide?


#3

yes i tried them … but not worked for me


#4

Maybe it is because you create the second BrowserWindow instance even if you don’t actually display it?


#5

So how i can make multiple windows application ???


#6

My assumption is that the existence of the second window is holding your application open until you release it, somehow. Apparently, pressing Ctrl+C is telling it to actually go away when you get into that “mostly exited” state.

This construct seems weird to me:

    newProject.on('close', function (event) {
        newProject.hide();
        event.preventDefault();
    });

The quick start guide shows this:

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })

Notice the difference between “close” in your code and “close d” in the quick start guide. In the quick start guide it doesn’t do anything other than release the window after it is already closed. What you do is when it receives a message saying, “Hey, I want you to close now” … you instruct it to not close, but hide instead. That’s where I suspect things are going wrong.

These are all just untested theories though, so ¯\_(ツ)_/¯


#7

ok i edited my code but still same problem

'use strict';
const electron = require('electron');
const app = electron.app;  // Module to control application life.
const BrowserWindow = electron.BrowserWindow;  // Module to create native browser window.
const ipc = electron.ipcMain;
const path = require('path');
var mainWindow = null;

app.on('window-all-closed', function() {
    if (process.platform != 'darwin')
        app.quit();
});

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

    //Main window
    const mainWindowPath = path.join('file://' + __dirname + '/app/index.html');
    mainWindow = new BrowserWindow({width: 1280, height: 700,minWidth:1280, minHeight: 700 ,maximizable : true});
    mainWindow.loadURL(mainWindowPath);
    mainWindow.on('closed', function () { 
        mainWindow = null;
     });

    //New project window
    const newProjectPath = path.join('file://'+ __dirname +'/project/project.html');
    var newProject = new BrowserWindow({width: 1280, height: 700 ,minWidth: 1280, minHeight: 700 ,maximizable : true,show: false})
    newProject.loadURL(newProjectPath);

    newProject.on('closed', function (event) {
       newProject = null;
    });

    ipc.on('show-project-win', function(){
        newProject.show();
    });

    //report page
    const reportPath = path.join('file://'+ __dirname +'/report/report.html');
    var report = new BrowserWindow({width: 1140, height: 584 ,minWidth: 1140, minHeight: 584,maxWidth: 1140, maxHeight: 584 ,maximizable : false,show: false})
    report.loadURL(reportPath);

    report.on('closed', function (event) {
        report = null;
    });

    ipc.on('show-report-win', function(){
        report.show();
    });

});

#8

Are you on a Mac and starting your app from the command line with electron .?


#9

no i’m on windows 7 64-bit