当Electron处于后台时避免应用节流 [英] Avoid app throttling when Electron is in background

查看:759
本文介绍了当Electron处于后台时避免应用节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下示例:

setInterval(function()
{
   console.log(new Date());
});

如果我使用 electron example.js运行它在OS X下,它会在我的工具栏中打开一个图标,并开始在控制台上打印时间。但是,如果应用程序没有集中精力,它会在一段时间后开始节流。

If I run it with electron example.js under OS X, it opens up an icon in my dockbar and starts printing out the time on the console. If the app is not focused, however, after a while it starts throttling.

我环顾四周,发现这是由于OS X的节能策略所致。现在,如果我需要它来继续在后台工作怎么办?我的应用程序将是一个守护程序,时不时地在做一些事情,而且我不能让用户长时间呆呆地盯着我的应用程序。

I looked around and found that this is due to OS X power saving strategy. Now, what if I needed it to keep working in background? My app will be a daemon doing a little bit of something every now and then, and I can't have my users blankly staring at my app for ages.

我发现出在这里我可以做的

electron.powerSaveBlocker.start('prevent-app-suspension');

实际上可以解决我的问题。但是,这是非常侵入性的,因为据我所知,它完全可以阻止系统进入睡眠状态!我并不需要那么多,我只需要我的应用在计算机处于活动状态且处于联机状态时做一些事情,而不必强迫它永远保持清醒状态。

Which actually fixes my problem. This however, is quite invasive, since as far as my understanding goes it prevents the system from sleeping at all! I don't need this much, I would just need my app to do something when the computer is active and online without forcing it to stay awake forever.

不是在让我的用户不断将应用程序保持在前台与让他们的计算机永远处于休眠状态之间,有什么中间方法?

Isn't there anything in the middle, between having my users keeping the app continuously in the foreground, and making their computer sleepless forever?

推荐答案

根据当前文档:


注意: prevent-display-sleep 具有更高的优先于
prevent-app-suspension 。只有最高优先级的类型才会生效。
换句话说,防止显示睡眠始终优先于
防止应用程序暂停

Note: prevent-display-sleep has higher precedence over prevent-app-suspension. Only the highest precedence type takes effect. In other words, prevent-display-sleep always takes precedence over prevent-app-suspension.

例如,调用A的API请求 prevent-app-suspension
另一个呼叫B请求 prevent-display-sleep
prevent-display-sleep 将一直使用到B停止其请求为止。在
之后,使用 prevent-app-suspension

For example, an API calling A requests for prevent-app-suspension, and another calling B requests for prevent-display-sleep. prevent-display-sleep will be used until B stops its request. After that, prevent-app-suspension is used.

这意味着将 prevent-app-suspension 设置为on,仍将允许系统进入睡眠状态,并按照您的意愿进行操作。但是,您可以运行该函数两次,同时传递两个标志,同时打开两个选项。但是,正如上面的文档所述,如果两个选项都都设置为on,那么在删除该标志之前,系统将不会进入睡眠状态。

What this means is that setting prevent-app-suspension to on, will still allow the system to sleep, and simply does what you desire it to. You can however run the function twice, passing both flags, turning both options on. However, as the docs above say, if both are set to on, then the system will not sleep until that flag has been removed.

这篇关于当Electron处于后台时避免应用节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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