NSTimer 中的代码阻止自动睡眠
我的应用程序中运行着一个 NSTimer,它收集一些数据并定期将其发送到服务器。 在生产中,计时器每隔几个小时就会触发一次。
我担心会干扰自动睡眠。 在测试中,定时器和睡眠时间的某些组合完全阻止了自动睡眠——显示器睡眠,系统保持运行。 将我的 NSTimer 设置为一分钟总是会停止它。
某些 Mac 应用程序因在运行时(或始终,如果安装了守护程序)干扰自动睡眠而臭名昭著。 哪些操作可以阻止系统休眠以及如何安全地运行定期任务?
I have an NSTimer
running in my application that collects some data and sends it to a server periodically. In production the timer will fire every few hours.
I am worried about interfering with automatic sleep. In testing, some combinations of timer and sleep time prevent automatic sleep entirely — the display sleeps, the system keeps running. Setting my NSTimer
to one minute always stops it.
Some Mac applications are notorious for interfering with automatic sleep when running (or all the time, if they install a daemon). What actions stop the system from sleeping and how can I run periodic tasks safely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSLog()
,至少在记录到/var/log/system.log
时,可以防止空闲睡眠。 我使用 launchd 守护进程进行了测试,该守护进程每分钟调用一次 NSLog(@"test"),并且系统睡眠空闲时间为 1 分钟,并且系统从未进入睡眠状态。 通过注释掉 NSLog 行,系统会在 1 分钟后进入睡眠状态。这是我为遇到此问题的其他人找到的唯一参考。
NSLog()
, at least when it is logging to/var/log/system.log
, can prevent idle sleep. I tested with a launchd daemon that would callNSLog(@"test")
every minute and with a system sleep idle time of 1 minute, and the system never went to sleep. By commenting out that NSLog line, the system went to sleep after 1 minute.Here's the only reference I found to someone else experiencing this issue.
根据 Apple 的文章“Mac OS X:为什么您的 Mac 可能不会”,访问磁盘将阻止您的计算机进入睡眠状态睡眠或保持睡眠模式”。
此外,我的测试表明,线程的优先级也会影响计算机是否睡眠。 以下带有计时器的代码将允许计算机进入睡眠状态。
删除 setThreadPriority 调用将阻止计算机休眠。
Accessing the disk will prevent your computer from sleeping, according to Apple's article "Mac OS X: Why your Mac might not sleep or stay in sleep mode".
Additionally, my testing has shown that the priority of a thread also has an impact on whether or not a computer will sleep. The following code with a timer will allow a computer to sleep.
Removal of the setThreadPriority call will prevent the computer from sleeping.
我有点困惑,请耐心等待。 =)
您的应用程序的活动将重置机器的睡眠计时器,因此除非传输之间的延迟大于不活动时间,否则机器不会进入睡眠状态。 我不完全确定 OS X 上什么被归类为“活动”,但如果它是类似 Linux 的东西,我希望网络或磁盘 IO 会像运行状态下的进程一样被计数——也就是说,处理数字或打乱数据在内存中。
另外,如果系统确实进入睡眠状态,您是否期望机器醒来以便您的应用程序可以与远程主机通信?
I'm a bit confused, bear with me. =)
Activity by your application will reset the machine's sleep timer, so unless the delay between transmits is larger than the inactivity period, the machine won't go to sleep. I'm not completely sure what classifies as "activity" on OS X, but if it's anything like Linux, I expect that network or disk IO would count as would processes in the running state - that is to say crunching numbers or shuffling data around in RAM.
Also, in the event the system did go to sleep, are you expecting the machine to wake up so your app can talk to the remote host?
你研究过launchd吗? http://developer.apple.com/MacOsX/launchd.html
这是什么操作系统用于其自己的服务,因此我确信睡眠已被考虑在内。
Have you looked into launchd? http://developer.apple.com/MacOsX/launchd.html
It is what the OS uses for its own services, so I'm sure sleep was taken into consideration.