如何在 Electron 应用程序中检测系统空闲并做一些事情

How to detect system idle in Electron app and do something

我开发了应用程序,如果用户在 30 分钟内什么都不做,我想关闭应用程序。

所以我在google上搜索解决这个问题,我发现electron中有'powerMonitor' API,但问题是我的应用程序是旧版本(Electron 3.1. 14) 所以不支持各种方法

Electron powerMonitor API Docs

我想我应该使用 powerMonitor.querySystemIdleTime() 方法并且我尝试测试 'pwoerMonitor' API 所以我粘贴了代码但是

console.log(powerMonitor.querySystemIdleTime())它returns这个错误信息。

如何检测系统空闲并在 electron 中做一些事情?我应该使用另一个包吗?

import { app, dialog, Menu, Notification, Tray, ipcMain, powerMonitor } from 'electron'
import windowManager from 'electron-window-manager'
import schedule from 'node-schedule'

let mainWindow = null
let tray = null
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\/g, '\\')
}
app.setAppUserModelId('Geomec Cloud Manager')
windowManager.init({
  appBase: process.env.NODE_ENV === 'production'
    ? `file://${__dirname}`
    : 'http://localhost:9080'
})
if (isSecondInstance) {
  app.quit()
} else {
  app.on('second-instance', () => {
    if (mainWindow) {
      mainWindow.object.show()
      mainWindow.object.focus()
    }
  })
  /* Main Window */
  app.on('ready', () => {
    mainWindow = windowManager.createNew('main-window', '', '/index.html', null, {
      title: 'Geomec Cloud Manager',
      height: 500,
      width: 500,
      useContentSize: true,
      frame: false,
      maximizable: false,
      minimizable: false,
      resizable: false
    }).create()
    console.log(powerMonitor.querySystemIdleTime()) // I thought it returns idle time
    if (!process.argv.includes('--systray')) {
      mainWindow.object.once('ready-to-show', () => {
        mainWindow.object.show()
      })
    }
    tray = new Tray(__static + '/tray-icon.ico')
    const trayMenu = Menu.buildFromTemplate([{
      label: '프로그램 정보',
      click () {
        const window = windowManager.createNew('about-window', '', '/index.html#about', null, {
          height: 380,
          width: 550,
          useContentSize: true,
          frame: false,
          maximizable: false,
          minimizable: false,
          resizable: false,
          modal: true,
          parent: windowManager.get('main-window').object
        }).create()
        window.object.once('ready-to-show', () => {
          window.object.show()
        })
      }
    },
    {
      /* Tray 내부 프로그램 종료 함수 */
      label: '프로그램 종료',
      click () {
        const notification = {
          icon: __static + '/' + 'app-icon.png',
          title: '네트워크 드라이브 연결이 해제되었습니다',
          body: '프로그램을 종료합니다'
        }
        new Notification(notification).show()
        mainWindow.object.webContents.send('terminate-child-processes')
        ipcMain.on('child-processes-terminated', () => {
          setTimeout(function () {
            app.quit()
          }, 5000)
        })
      }
    }
    ])
    tray.setToolTip('Geomec Cloud Manager')
    tray.setContextMenu(trayMenu)
    tray.on('click', () => {
      mainWindow.object.show()
    })
  })
}


根据文档,调用是:powerMonitor.querySystemIdleTime(callback) 并且如错误消息所述,您没有提供所需的参数。

尝试更改:

console.log(powerMonitor.querySystemIdleTime())

至:

powerMonitor.querySystemIdleTime((idleSecs)=>{
  console.log(`The system has been idle for ${idleSecs} seconds.`)
  })

根据您的用例,您实际上需要使用 powerMonitor.querySystemIdleState(idleThreshold, callback)idleThreshold 设置为 30 * 60