Re: Getting Notification of Sleep
Re: Getting Notification of Sleep
- Subject: Re: Getting Notification of Sleep
- From: Andreas Monitzer <email@hidden>
- Date: Sat, 29 Sep 2001 02:43:22 +0200
On Saturday, September 29, 2001, at 02:26 , Ed Baskerville wrote:
I'm writing an app that uses NSTimers to schedule messages at particular
times, but when the computer goes to sleep, the timers stop
running...meaning that if something's scheduled for 10 PM, and you put
the computer to sleep for half an hour, the message won't get sent until
10:30. Is there any way to get notification that the computer just woke
from sleep from Cocoa? Or if not from Cocoa, from Carbon?
It's possible to do in Carbon, but the IOKit-way fits better into a
Cocoa-app (it uses CFRunLoop).
Here's some example code:
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
// to initialize:
CFRunLoopSourceRef rls;
IONotificationPortRef thePortRef;
io_object_t notifier, result;
result=IORegisterForSystemPower( NULL, &thePortRef, sleepNotification,
¬ifier );
if(result==0)
NSLog(@"IORegisterForSystemPower failed.");
rls = IONotificationPortGetRunLoopSource(thePortRef);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
CFRelease(rls);
// to deinitialize:
IODeregisterForSystemPower(¬ifier);
void sleepNotification(
void * refcon,
io_service_t service,
natural_t messageType,
void * messageArgument )
{
switch(messageType)
{
case kIOMessageSystemWillSleep:
NSLog(@"Sleep");
break;
case kIOMessageSystemHasPoweredOn:
NSLog(@"Awoken");
break;
}
}
You'll have to include the IOKit and CoreFoundation-framework.
andy
--
Description forthcoming.