2016-11-06 38 views
0

我到处搜索过,但我找不到如何创建我的电子应用程序的多个实例。我正在尝试创建一个便签应用程序,其中有一个加窗口,用于创建一个新的便笺,而该便笺又将具有相同的加号按钮。在Electron中创建应用程序的多个实例

enter image description here

我都试过,但没有对第一次点击便签一个新的实例被创建,但在第二次点击加号按钮2新的实例被创建并在第三次加号按钮一个巨大的问题点击加号按钮4新的实例被创建

里面main.js文件

let mainWindow 

function createWindow() { 
    // Create the browser window. 
    mainWindow = new BrowserWindow({width: 190, height: 190,frame:false,transparent:true}) 

    mainWindow.loadURL(url.format({ 
    pathname: path.join(__dirname, 'index.html'), 
    protocol: 'file:', 
    slashes: true 
    })) 
    mainWindow.on('closed', function() { 
    mainWindow = null 
    }) 
    //when plus button is clicked it is firing a message 'create-new-instance' through ipcRenderer 
    ipcMain.on('create-new-instance',()=>{ 
    console.log('create new window'); 
    createWindow(); 
    }) 
} 

//this is called when for the first time the app is launched 
app.on('ready', createWindow) 

里面renderer.js文件,其中加号按钮位于

document.getElementById('plusButton').addEventListener("click",()=>{ 
    var {ipcRenderer} = require('electron') 
    ipcRenderer.send('create-new-instance') 
}) 

问题是,当我第一次点击加号按钮“创建新实例”被触发,因为有说明的只有一个实例(当你第一次启动应用程序时创建)创建一个新的笔记,使它的两个实例都能够听'create-new-instance'事件,当我第二次点击加按钮时,这两个笔记听这个事件并创建1注意每个使它总共4个音符。

请帮助我或者给我建议任何其他替代方法怎么办呢:)

回答

1

这里的主要问题是,您要添加的侦听器内createWindow()create-new-instance事件,所以每次createWindow()运行它增加另一个事件侦听器,并且当create-new-instance发出时,每个侦听器都会创建一个新窗口。为避免这种情况,您需要将事件订阅移到createWindow()之外,以便无论调用多少次createWindow()都只有一个事件监听器。

+0

工作,一个应用程序的每个实例分配55MB的物理内存是可以的,但如果用户打开10个应用程序实例,则会增加高达500 + MB的物理存储空间。有没有其他方法可以让你知道创建了多个应用程序实例,但实际上只有一个进程正在进行操作 –

相关问题