• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting Notification of Sleep
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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,
&notifier );

if(result==0)
NSLog(@"IORegisterForSystemPower failed.");

rls = IONotificationPortGetRunLoopSource(thePortRef);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
CFRelease(rls);

// to deinitialize:
IODeregisterForSystemPower(&notifier);


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.


References: 
 >Getting Notification of Sleep (From: Ed Baskerville <email@hidden>)

  • Prev by Date: NSImage/ImageRep/ImageView
  • Next by Date: Re: Makin' frameworks
  • Previous by thread: Getting Notification of Sleep
  • Next by thread: NSImage/ImageRep/ImageView
  • Index(es):
    • Date
    • Thread