关闭软件同时,子进程也退出

经过两天的各种文档查找,最终放弃第一种,只能选择beforeUnload方式来处理。

失败的一种方式:
vue中
methods:

closeServe(pid) {
    const execClose = `taskkill /pid ${pid} -t -f`;

    $childProcess.exec(execClose, (error, stdout, stderr) => {
        if (error) {
            console.log(error.stack);
            console.log('Error code: '+error.code);
            return;
        }
    })
}

mounted:

$remote.getCurrentWindow().on('close', () => {
  
    this.closeServe(this.projectList[0].execChild);
    $remote.getCurrentWindow().removeAllListeners();
})  

成功的方式:

let closeWindow = false

            window.addEventListener('beforeunload', evt => {
                if (closeWindow) return
                logFile(`[currentWindow beforeunload]`);
                evt.returnValue = false

                setTimeout(() => {
                    let result = $dialog.showMessageBox({
                        message: '是否确认退出应用?',
                        buttons: ['是', '否']
                    })
                    logFile(`[currentWindow beforeunload result] ${result}`);
                    if (result == 0) {
                        closeWindow = true

                        if(this.projectList.length) {
                    
                            this.projectList.forEach((item, index) => {

                                //不管是否启动都执行
                                item.isActive = false;
                                localStorage.setItem('auto_project_collection', JSON.stringify(this.projectList));
                                
                                logFile(`[currentWindow close forEach] ${item.isActive}`);    
                                this.closeServe(item, index, true);
                            });

                        } 

                        $currentWindow.close();
                        $currentWindow.removeAllListeners('close');

                    }
                })
            })

这个方法需要在入口的vue组件中,否则会重复多次监听,导致多次出现是否关闭的提示弹窗。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 下载安装搭建环境 可以选npm安装,或者简单下载一个开发版的vue.js文件 浏览器打开加载有vue的文档时,控制...
    冥冥2017阅读 11,284评论 0 42
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 10,489评论 0 29
  • 有好多话想说,却不知要从何说起。 本以为早已忘记,没想到再次听到还是会扰乱自己的心。 想要当一个无心之人,不想再被...
    小太阳04阅读 1,400评论 0 1
  • 在这习惯用微信的时代,用QQ的频率似乎越来越少,现在的QQ对于我而言更多的作用是储蓄文件,但我依然会时不时...
    a陈姣阅读 4,750评论 0 1
  • 我经常回市郊的老家,路上会途经一个小集镇“漆桥”。小时候,那里还只是一个名不见经传的小镇,但经过几十年的发展变化,...
    慢城wucheng阅读 2,701评论 2 2

友情链接更多精彩内容