Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!