【electron-vue】进程无法杀死导致任务管理器资源飙升,电脑卡死的情况(改良版)

前景:同一个electron-vue项目,别人的电脑不会,而我编辑代码ctrl+s几下,后台8、9个进程,CPU 100%。

解决:(不想知道过程的,直接拉到最后,看最终修改的代码)

1、参考【Electron-Vue 】进程无法杀死导致任务管理器资源飙升,电脑卡死的情况。

作为elctron-vue菜鸟,配置一窍不通,好不容易搜到这个文章,复制拷贝,运行ctrl+s报错。

        exec(`TASKKILL /F /IM electron.exe`, function (err, data) {
        ^

ReferenceError: exec is not defined
    at Watching.compiler.watch [as handler] (E:\electronTest\loginnew\.electron-vue\dev-runner.js:103:9)
    at compiler.hooks.done.callAsync (E:\electronTest\loginnew\node_modules\webpack\lib\Watching.js:99:9)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (E:\electronTest\loginnew\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Watching._done (E:\electronTest\loginnew\node_modules\webpack\lib\Watching.js:98:28)
    at compiler.emitRecords.err (E:\electronTest\loginnew\node_modules\webpack\lib\Watching.js:73:19)
    at Compiler.emitRecords (E:\electronTest\loginnew\node_modules\webpack\lib\Compiler.js:499:39)
    at compiler.emitAssets.err (E:\electronTest\loginnew\node_modules\webpack\lib\Watching.js:54:20)
    at hooks.afterEmit.callAsync.err (E:\electronTest\loginnew\node_modules\webpack\lib\Compiler.js:485:14)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (E:\electronTest\loginnew\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
    at asyncLib.forEachLimit.err (E:\electronTest\loginnew\node_modules\webpack\lib\Compiler.js:482:27)
    at E:\electronTest\loginnew\node_modules\neo-async\async.js:2818:7
    at done (E:\electronTest\loginnew\node_modules\neo-async\async.js:3522:9)

 

2、一看这报错不就是我刚拷贝上的代码么。

参考:nodeJS exec does not work for “cd ” shell cmd

原作者的文章里少了:

var exec =  require('child_process').exec;

 

3、但是!运行,ctrl+s,终端只有一句 kill pid: 27792 success! 

我要的效果是:运行,ctrl+s,干掉多余的进程,重新运行。对比代码修改,发现最初的代码有个延时

setTimeout(() => {
            manualRestart = false
          }, 5000)

 

4、dev-runner.js 修改代码

if (electronProcess && electronProcess.kill) {
        manualRestart = true
        const pid = electronProcess.pid
        var exec =  require('child_process').exec;
        exec(`TASKKILL /F /IM electron.exe`, function (err, data) {
          if (err) console.log(err)
          else console.log('kill pid: ' + pid + ' success!')
          electronProcess = null
          startElectron()
          setTimeout(() => {
            manualRestart = false
          }, 5000)
        })
      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Electron-Vue进程通讯可以通过使用 `ipcRenderer` 和 `ipcMain` 进行实现。 `ipcRenderer` 是在渲染进程中使用的模块,它负责发送异步消息到主进程中的 `ipcMain`,并且接收主进程回复的消息。使用 `ipcRenderer.send(channel, args)` 向主进程发送消息,使用 `ipcRenderer.on(channel, listener)` 监听主进程发送过来的消息。 `ipcMain` 是在主进程中使用的模块,它负责接收来自渲染进程发送过来的异步消息,并且处理消息后通过 `event.sender.send()` 方法向渲染进程发送回复消息。使用 `ipcMain.on(channel, listener)` 监听渲染进程发送过来的消息。 下面是一个简单的示例,演示了如何在渲染进程和主进程之间进行简单的通讯: ```js // 在渲染进程中 const { ipcRenderer } = require('electron') ipcRenderer.send('message', 'Hello World!') ipcRenderer.on('reply', (event, arg) => { console.log(arg) }) // 在主进程中 const { ipcMain } = require('electron') ipcMain.on('message', (event, arg) => { console.log(arg) event.sender.send('reply', 'Hi there!') }) ``` 在上面的示例中,渲染进程通过 `ipcRenderer.send()` 方法向主进程发送了一个消息,主进程通过 `ipcMain.on()` 方法监听渲染进程发送过来的消息,处理完消息后通过 `event.sender.send()` 方法向渲染进程发送了一个回复消息。渲染进程通过 `ipcRenderer.on()` 方法监听主进程发送过来的回复消息,并且在控制台输出了回复消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值