最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating invisible window from main process to compute function in Electron - Stack Overflow

programmeradmin2浏览0评论

I am trying to write an Electron program in which the main process creates an invisible window, sends a number to that window, the window putes the factorial and then sends it back.

This is in the main process:

function invisibleWindow() {
  const invisPath = 'file://' + path.join(__dirname, 'files/html/inv.html')

  let win = new BrowserWindow({ width: 400, height: 400, show: false })
  win.loadURL(invisPath)

  win.webContents.on('did-finish-load', function () {
    const input = 100;
    win.webContents.send('pute-factorial', input);
  })



  ipcMain.on('factorial-puted', function (event, input, output) {
    const message = `The factorial of ${input} is ${output}`
    console.log(message);
  })
}

The function gets called in the main process by:

app.on('ready', () => {
  // creates different window here

  invisibleWindow();
});

This is the inv.html file:

<html>
  <script type="text/javascript">
    const ipc = require('electron').ipcRenderer
    const BrowserWindow = require('electron').remote.BrowserWindow

    ipc.on('pute-factorial', function (event, number) {
      const result = factorial(number)

      ipcRenderer.send('factorial-puted', number, result)
      window.close()
    })

    function factorial (num) {
      if (num === 0) return 1
      return num * factorial(num - 1)
    }
  </script>
</html>

Now after I have added this to my program, every time when I start it through the terminal, it does not terminate by itself even when all other (visible) windows are closed. I guess this is because the invisible window is still opened because it did not receive the pute-factorial event.

What am I doing wrong?

I am trying to write an Electron program in which the main process creates an invisible window, sends a number to that window, the window putes the factorial and then sends it back.

This is in the main process:

function invisibleWindow() {
  const invisPath = 'file://' + path.join(__dirname, 'files/html/inv.html')

  let win = new BrowserWindow({ width: 400, height: 400, show: false })
  win.loadURL(invisPath)

  win.webContents.on('did-finish-load', function () {
    const input = 100;
    win.webContents.send('pute-factorial', input);
  })



  ipcMain.on('factorial-puted', function (event, input, output) {
    const message = `The factorial of ${input} is ${output}`
    console.log(message);
  })
}

The function gets called in the main process by:

app.on('ready', () => {
  // creates different window here

  invisibleWindow();
});

This is the inv.html file:

<html>
  <script type="text/javascript">
    const ipc = require('electron').ipcRenderer
    const BrowserWindow = require('electron').remote.BrowserWindow

    ipc.on('pute-factorial', function (event, number) {
      const result = factorial(number)

      ipcRenderer.send('factorial-puted', number, result)
      window.close()
    })

    function factorial (num) {
      if (num === 0) return 1
      return num * factorial(num - 1)
    }
  </script>
</html>

Now after I have added this to my program, every time when I start it through the terminal, it does not terminate by itself even when all other (visible) windows are closed. I guess this is because the invisible window is still opened because it did not receive the pute-factorial event.

What am I doing wrong?

Share Improve this question asked Sep 11, 2017 at 6:49 niklasscniklassc 5502 gold badges13 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

This is because of an race condition. Electron docs:

Event: 'did-finish-load'

Emitted when the navigation is done, i.e. the spinner of the tab has stopped spinning, and the onload event was dispatched.

You can try it with setTimeout:

win.webContents.on('did-finish-load', function () {
  setTimeout(() => {
    const input = 100;
    win.webContents.send('pute-factorial', input);
  }, 3000);
});

The main process does not know when the DOM is ready. You can do something like this.

Send your main process an "dom-is-ready" event.

inv.html

ipc.send('dom-is-ready');

Paste your 'did-finish-load' code into 'dom-is-ready'.

main.js

function invisibleWindow() {
  const invisPath = 'file://' + path.join(__dirname, 'files/html/inv.html');

  const win = new BrowserWindow({ 
    width: 400, 
    height: 400, 
    show: false 
  });

  win.loadURL(invisPath);

  win.webContents.on('did-finish-load', function () {
    win.show();
  });

  ipcMain.on('dom-is-ready', function (event) {
    const input = 100;
    win.webContents.send('pute-factorial', input);
  });

  ipcMain.on('factorial-puted', function (event, input, output) {
    const message = `The factorial of ${input} is ${output}`
    console.log(message);
  });
}
发布评论

评论列表(0)

  1. 暂无评论