使用Electron处理表单崩溃和挂起。本系列文章的应用示例已发布在GitHub上:electron-api-demos-Zh_CN。您可以克隆或下载并运行查看。WelcomeStar.BrowserWindow模块会在渲染器进程事件发出时崩溃或挂起。您可以监听这些事件并让用户有机会重新加载、等待或关闭窗口。在浏览器中打开完整的API文档。进程崩溃后重新加载表单支持:Win、macOS、Linux|Process:Main在此示例中,我们创建了一个新窗口(通过远程模块)并提供了一个链接以使用process.crash()方法强制崩溃。当前窗口正在侦听崩溃事件,当此事件发生时,它为用户提供了两个选项:重新加载或关闭。渲染进程constBrowserWindow=require('electron').remote.BrowserWindowconstdialog=require('electron').remote.dialogconstpath=require('path')constprocessCrashBtn=document.getElementById('process-crash')processCrashBtn.addEventListener('click',function(event){constcrashWinPath=path.join('file://',__dirname,'../../sections/windows/process-crash.html')让win=newBrowserWindow({width:400,height:320})win.webContents.on('crashed',function(){constoptions={type:'info',title:'RendererprocessCrash',message:'此进程已崩溃。',buttons:['Reload','Close']}dialog.showMessageBox(options,函数(索引){如果(索引===0)赢。重新加载()否则赢了。关闭()})})赢。on('close',function(){win=null})获胜。loadURL(crashWinPath)win.show()})进程挂起后重新加载窗口支持:Win、macOS、Linux|Process:Main在此示例中,我们创建了一个新窗口(通过远程模块)并使用process.hang()方法提供一个窗口以强制链接挂起进程。当前表单正在侦听以查看进程是否真的没有响应(这可能最多需要30秒)。当此事件发生时,它为用户提供了两个选项:重新加载或关闭。渲染进程constBrowserWindow=require('electron').remote.BrowserWindowconstdialog=require('electron').remote.dialogconstpath=require('path')constprocessHangBtn=document.getElementById('process-hang')processHangBtn.addEventListener('click',function(event){consthangWinPath=path.join('file://',__dirname,'../../sections/windows/process-hang.html')letwin=newBrowserWindow({width:400,height:320})win.on('unresponsive',function(){constoptions={type:'info',title:'Rendererprocessissuspended',message:'此进程已暂停。',buttons:['Reload','Close']}dialog.showMessageBox(options,function(index){if(index===0)赢。重新加载()否则赢了。关闭()})})赢。on('close',function(){win=null})获胜。loadURL(hangWinPath)win.show()})等待进程再次响应的高级技巧。对于挂起的进程,第三种选择是等待,看看问题是否已解决,让进程再次响应。为此,请使用BrowserWindow的“responsive”事件,如下所示:win.on('responsive',function(){//当窗口再次响应时执行某些操作})如果本文对您有帮助,请点赞或加星GitHub:electron-api-demos-Zh_CN支持,谢谢。
