Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda