关闭应用程序并通知渲染器进程 [英] Closing application and notifying renderer process

查看:93
本文介绍了关闭应用程序并通知渲染器进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Electron应用程序,它在用户关闭时需要保存一些数据(例如,在用户单击关闭按钮之后)。

I have an Electron application that needs to save some data when it's closed by the user (e.g. just after the user clicked on the "Close" button).

数据在渲染器过程中可用,因此应在应用程序终止之前通知它们。

The data is available at the renderer process, so it should be notified before the application dies.

用于浏览器窗口的电子API 提到了 close 方法,但似乎这

The Electron API for Browser Window mentions a close method, but it seems this is done by the main process, not the renderer one (if I'm not mistaken).

我尝试使用 WebContents.send 从主进程通知渲染器进程,但似乎由于消息是异步的,因此在渲染器进程有时间实际执行操作之前,应用程序已关闭。

I tried using WebContents.send from the main process to notify the renderer process, but it seems that, because the message is asynchronous, the application is closed before the renderer process has the time to actually perform the operations.

推荐答案

您可以只使用常规的 卸载 beforeunload 渲染器过程中的事件:

You can just use the normal unload or beforeunload events in the renderer process:

window.addEventListener('unload', function(event) {
  // store data etc.
})

这篇关于关闭应用程序并通知渲染器进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆