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

javascript - Acquire renderer process id in Electron - Stack Overflow

programmeradmin0浏览0评论

In Electron, the renderer process's pid was exposed by

processId = require('remote').getCurrentWindow().getProcessId()

which, however, is no longer valid in recent releases (1.4.x, 1.5.x, 1.6.x).

Is there any other way to get the pid of the renderer process, i.e. the pid for the Windows ?

In Electron, the renderer process's pid was exposed by

processId = require('remote').getCurrentWindow().getProcessId()

which, however, is no longer valid in recent releases (1.4.x, 1.5.x, 1.6.x).

Is there any other way to get the pid of the renderer process, i.e. the pid for the Windows ?

Share Improve this question edited May 17, 2017 at 18:26 hackjutsu asked May 15, 2017 at 22:13 hackjutsuhackjutsu 8,92215 gold badges51 silver badges92 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 6

The method getOSProcessId() to acquire the renderer's OS pid (not the routing id) was added to Electron v1.7.1. Here is the original pull request.

require('electron').remote.getCurrentWebContents().getOSProcessId();

Strangely enough, on Darwin or Linux Mint, with Electron 1.6.7,

require('electron').remote.getCurrentWebContents().getProcessId()

returns 3, which seems quite small for a valid process id.

However, from the renderer process,

process.pid

returns the correct renderer process id, and

require('electron').remote.process.pid

returns the correct main process id.

This can be confirmed by using the Activity Monitor application on Darwin, or the System Monitor application on Linux Mint.

The following slightly modified version works for me

require('electron').remote.getCurrentWebContents().getProcessId()

Example:

const { app, BrowserWindow } = require('electron')

app.once('ready', () => {
  var br = new BrowserWindow()
  br.once('focus', () => {
    br.webContents.openDevTools({detach:true})
    br.webContents.executeJavaScript(`
      const remote = require('electron').remote
      console.log(remote.getCurrentWebContents().getProcessId())
    `)
  })
  br.loadURL('http://google.com')
})

Tested on 1.4.13

As of electron 14 the remote module has been removed

// Deprecated in Electron 12:
const { BrowserWindow } = require('electron').remote


// Replace with:
const { BrowserWindow } = require('@electron/remote')

// In the main process:
require('@electron/remote/main').initialize()
发布评论

评论列表(0)

  1. 暂无评论