Electron主进程 管理 所有网页及其对应的渲染进程,而渲染进程只能管理相应的网页, 一个渲染进程的崩溃不会影响其他渲染进程;那么两个进程之间如何通讯呢?
Electron 有两种进程:主进程和渲染进程。
主进程
1. 主进程通过创建 BrowserWindow 实例来创建 网页。 每一个 BrowserWindow 实例在其渲染过程中运行网页, 当一个 BrowserWindow 实例被销毁时,对应的渲染过程也会被终止。
2.主进程 管理 所有网页及其对应的渲染进程。
渲染进程
1. 渲染进程只能管理相应的网页, 一个渲染进程的崩溃不会影响其他渲染进程。
2. 渲染进程通过 IPC 与主进程通信在网在页上执行 GUI 操作。 出于安全和可能的资源泄漏考虑,直接从渲染器进程中调用与本地 GUI 有关的 API 受到限制。
进程之间的通信可以通过 Inter-Process Communication(IPC) 模块进行:ipcMain 和 ipcRenderer
若要创建一个窗口,请调用 BrowserWindow 类,但只能在主进程中使用(main.js):
const { BrowserWindow } = require('electron') const win = new BrowserWindow()
若要从渲染进程调用主进程,请使用 IPC 模块: 例如净化窗口模式下自定义关闭窗口功能
1. 首先由主进程向渲染进程发送消息,告诉它窗口的id(开启了多个渲染进程);
win.once('ready-to-show', () => { win.webContents.send('init_win_id', win.id); win.show() });
2. 在渲染进程接收winid,并在关闭窗口时发送给主进程
//渲染进程监听init_win_id事件(自定义),将winid赋给全局变量winid let winid = ''; require('electron').ipcRenderer.on('init_win_id', (event, message) => { winid = message; }) // 关闭窗体 function closex(){ //渲染进程里把窗口id发给主进程 const { ipcRenderer } = require('electron') ipcRenderer.send('close_window', winid); }
3. 在主进程中接收需要关闭的渲染进程
// 主进程获取winid关闭窗体 ipcMain.on('close_window', (event, arg) => { //app.quit()单个窗口可以直接app.quit() //win.close(); // 1、接收渲染进程发来的窗口id let winid = arg; console.log(winid); // 2、通过id找到对应的BrowserWindow对象(win、win22),调用这个对象的close()关闭对应的窗口 BrowserWindow.fromId(winid).close(); })
以上是electron制作QQ音乐客户端-主进程与渲染进程通讯的详细内容。更多信息请关注PHP中文网其他相关文章!

Include:1)AsteeplearningCurvedUetoItsVasteCosystem,2)SeochallengesWithClient-SiderEndering,3)潜在的PersperformanceissuesInsuesInlArgeApplications,4)ComplexStateStateManagementAsappsgrow和5)TheneedtokeEedtokeEedtokeEppwithitsrapideDrapidevoltolution.thereedtokeEppectortorservolution.thereedthersrapidevolution.ththesefactorsshesssheou

reactischallengingforbeginnersduetoitssteplearningcurveandparadigmshifttocoment oparchitecent.1)startwithofficialdocumentationforasolidFoundation.2)了解jsxandhowtoembedjavascriptwithinit.3)

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

javascriptfatigueinrectismanagbaiblewithstrategiesLike just just in-timelearninganning and CuratedInformationsources.1)学习whatyouneedwhenyouneedit

totlecteactComponents通过theusestatehook,使用jestandReaCtteTingLibraryToSigulation Interactions andverifyStatAtaTeChangesInTheUI.1)renderthecomponentAndComponentAndComponentAndCheckInitialState.2)模拟useclicklicksorformsormissionsions.3)

KeysinreactarecrucialforopTimizingPerformanceByingIneFefitedListupDates.1)useKeyStoIndentifyAndTrackListelements.2)避免使用ArrayIndicesasKeystopreventperformansissues.3)ChooSestableIdentifierslikeIdentifierSlikeItem.idtomaintainAinainCommaintOnconMaintOmentStateAteanDimpperperFermerfermperfermerformperfermerformfermerformfermerformfermerment.ChosestopReventPerformissues.3)

ReactKeySareUniqueIdentifiers usedwhenrenderingListstoimprovereConciliation效率。1)heelPreactrackChangesInListItems,2)使用StableanDuniqueIdentifiersLikeItifiersLikeItemidSisRecumended,3)避免使用ArrayIndicesaskeyindicesaskeystopreventopReventOpReventSissUseSuseSuseWithReRefers和4)

独特的keysarecrucialinreactforoptimizingRendering和MaintainingComponentStateTegrity.1)useanaturalAlaluniqueIdentifierFromyourDataiFabable.2)ifnonaturalalientedifierexistsistsists,generateauniqueKeyniqueKeyKeyLiquekeyperaliqeyAliqueLiqueAlighatiSaliqueLiberaryLlikikeuuId.3)deversearrayIndiceSaskeyseSecialIndiceSeasseAsialIndiceAseAsialIndiceAsiall


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版
中文版,非常好用

Dreamweaver Mac版
视觉化网页开发工具