Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
Electron - close renderer from main handler

TLDR: Best way to close a renderer window from main (or the renderer itself).

I am splitting up a process between X invisible renderer windows. When a renderer finishes its work, I want it to send an event to the main process and then close. Right now I have

//invisibleRenderer.js
doStuff().then(() => {
  ipcRenderer.invoke('finish');
}

What is the best way to close the window? Is it in the ipcMain.handle? I can't figure out which method to call with which id.

ipcMain.handle('finish', (event, args) => {
  //do what? event.frameId, event.processId, event.sender.id...
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use BrowserWindow.fromId(event.sender.id) to get a reference to the window from where the IPC call originated, as demonstrated in this question.

This works fine in the main process. The following example opens 5 windows and loads the same contents. Each renderer script invocation picks a random delay (to simulate some work) and then dispatches the finish message to the main process. There, the main process picks up the BrowserWindow reference using event.sender.id and closes the corresponding window.

app.js

const {app, BrowserWindow, ipcMain} = require("electron");

const windows = [];

app.on("ready", function() {
    for (let i=0; i<5; i++) {
        let win;
        win = new BrowserWindow( {
            width: 300,
            height: 300,
            x: i*100,
            y: i*100,
            webPreferences: {
                nodeIntegration: true,
                contextIsolation: false
            },
        });

        win.loadURL(`file://${__dirname}/index.html`);
        windows.push(win);
    }
});

ipcMain.handle("finish", (event, args) => {
    const win = BrowserWindow.fromId(event.sender.id);
    win.close();
});

index.html

<html>
<body>
Renderer
</body>
<script type="text/javascript" src="renderer.js"></script>
</html>

renderer.js

const {ipcRenderer} = require("electron");
// Simulate some work by choosing a random delay..
const delay = Math.round(Math.random()*10000);
setTimeout(() => ipcRenderer.invoke("finish"), delay);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda