Browse Source

update: 继续完善electron调起外部应用及获取执行结果示例

master
Poised_flw 4 years ago
parent
commit
934cb6d2f8
  1. 28
      app/main/child_process.js
  2. 11
      app/main/ipc.js

28
app/main/child_process.js

@ -1,17 +1,37 @@
const { spawn } = require('child_process')
let pidMap = {}
function start(command, args) {
/**
* options: { onSuccess: null | function, onError: null | function, onClose: null | function }
*/
function start(command, args, options = {}) {
const childProgress = spawn(command, args)
pidMap[childProgress.pid] = childProgress
childProgress.stdout.on('data' data => {
options.onSuccess && options.onSuccess(data)
})
childProgress.stderr.on('data', data => {
options.onError && options.onError(data)
})
childProgress.on('close', code => {
options.onClose && options.onClose(code)
})
return childProgress.pid
}
function kill(pid) {
const childProgress = pidMap[pid]
if (!childProgress || childProgress.killed) return;
// TODO 需要测试是否能真正杀掉
return childProgress.kill()
let status = false
if (childProgress && !childProgress.killed) {
status = childProgress.kill()
}
childProgress = null
delete pidMap[pid]
return status
}
module.exports = {

11
app/main/ipc.js

@ -211,15 +211,20 @@ module.exports = () => {
//
// ipcMain.handle('removeApp', removeApp);
/*
/* ipcRenderer
ipcRenderer.send('spawn', {
command: '微信.exe',
args: null || ['--disable-cache'],
key: + new Date // 唯一key,用来做关闭识别
key: + new Date // 唯一key,用来做关闭识别,
options: {
onSuccess: data => {}, // 正常执行回调
onError: err => {}, // 错误执行回调
onClose: code => {}, // 关闭回调
}
})
*/
ipcMain.on('spawn', (event, arg) => {
const pid = childProcess.start(arg.command, arg.args)
const pid = childProcess.start(arg.command, arg.args, arg.options)
event.sender.send('spawn-success', {
key: arg.key,
pid

Loading…
Cancel
Save