当 Electron 在后台时避免应用程序限制 [英] Avoid app throttling when Electron is in background

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

问题描述

考虑以下示例:

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

如果我在 OS X 下使用 electron example.js 运行它,它会在我的停靠栏中打开一个图标并开始在控制台上打印出时间.但是,如果应用没有集中注意力,过一会儿它就会开始节流.

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-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.

例如,API 调用 A 请求 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天全站免登陆