防止OS X上的睡眠的正确方法是什么? [英] What is the correct way to prevent sleep on OS X?

查看:132
本文介绍了防止OS X上的睡眠的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

当前版本的OS X(10.7)上防止应用程序或进程运行时防止睡眠的正确方法是什么?

What is the correct method on the current version of OS X (10.7) for preventing sleep while an application or process is running?

尤其是 IOCancelPowerChange 仍然(或曾经)达到了这个目的吗?我调用 IOCancelPowerChange 来响应 kIOMessageCanSystemSleep ,但这不能解决问题。

In particular, is IOCancelPowerChange still (or did it ever) serve this purpose? I call IOCancelPowerChange in response to kIOMessageCanSystemSleep, but that doesn't do the trick.

基本上是,但是文档指向的文档已经很旧了,答案从未被接受。

Essentially the same question as the first part of this one has been asked before, but the documentation it points to is quite old and the answer was never accepted.

推荐答案

IOCancelPowerChange 继续工作,但仅适用于空闲触发的睡眠;

IOCancelPowerChange continues to work but only for idle triggered sleep; it will not work for sleep triggered by the Finder's Sleep menu item, programmatically requested, or from a push of the power button.

Apple的Q&A1340 回答以下问题:问:当计算机进入睡眠状态或从唤醒状态唤醒时,如何通知我的应用程序睡眠?如何防止睡眠?

Apple's Q&A1340 answers the question "Q: How can my application get notified when the computer is going to sleep or waking from sleep? How do I prevent sleep?"

Q&A1340

#import <IOKit/pwr_mgt/IOPMLib.h>

// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
// kIOPMAssertionTypeNoIdleSleep prevents idle sleep

//reasonForActivity is a descriptive string used by the system whenever it needs 
//  to tell the user why the system is not sleeping. For example, 
//  "Mail Compacting Mailboxes" would be a useful string.

//  NOTE: IOPMAssertionCreateWithName limits the string to 128 characters. 
CFStringRef* reasonForActivity= CFSTR("Describe Activity Type");

IOPMAssertionID assertionID;
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, 
                                    kIOPMAssertionLevelOn, reasonForActivity, &assertionID); 
if (success == kIOReturnSuccess)
{

    //Add the work you need to do without 
    //  the system sleeping here.

    success = IOPMAssertionRelease(assertionID);
    //The system will be able to sleep again. 
}

请注意,您只能停止空闲时间睡眠,不能停止用户触发的睡眠。

Note that you can only stop idle time sleep, not sleep triggered by the user.

对于支持Mac OS X 10.6及更高版本的应用程序,请使用新的 IOPMAssertion 系列功能。这些功能使其他应用程序和实用程序可以看到您的应用程序不睡觉的愿望;这对于与第三方电源管理软件无缝协作至关重要。

For applications supporting Mac OS X 10.6 and later, use the new IOPMAssertion family of functions. These functions allow other applications and utilities to see your application's desire not to sleep; this is critical to working seamlessly with third party power management software.

这篇关于防止OS X上的睡眠的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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