NSWorkspaceWillSleepNotification not received
NSWorkspaceWillSleepNotification not received
- Subject: NSWorkspaceWillSleepNotification not received
- From: "Dragana Radulovic" <email@hidden>
- Date: Mon, 26 Nov 2007 16:32:47 -0500
- Thread-topic: NSWorkspaceWillSleepNotification not received
Hi,
I'm trying to create an application that will have a separate thread to monitor some of the resources. I would like this thread to stop running when my application receives NSWorkspaceWillSleepNotification and start again after receiving NSWorkspaceDidWakeNotification.
When I run my application from the development environment it all works fine (XCode version 2.4.1). But when I try running it outside of dev environment it never receives NSWorkspaceWillSleepNotification and my machine never goes to sleep (my Energy Saver settings are set to the minimum wait time).
My machine is MacBook Pro with Mac OS X version 10.4.10
What am I doing wrong?
Any help would be greatly appreciated.
Here is my code (source of my only class - "SleepNotif"):
#import "SleepNotif.h"
@implementation SleepNotif
- (id)init{
[super init];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self selector:@selector(didWakeNotification:) name: @"NSWorkspaceDidWakeNotification" object: nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self selector:@selector(willSleepNotification:) name: @"NSWorkspaceWillSleepNotification" object: nil];
[[NSApplication sharedApplication] setDelegate:self];
mRunThreadFlag = FALSE;
mCounter = 0;
return self;
}
- (void)dealloc{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[super dealloc];
}
- (void)didWakeNotification:(NSNotification *)notification{
NSLog(@"didWakeNotification: received NSWorkspaceDidWakeNotification");
[self startThread];
}
- (void)willSleepNotification:(NSNotification *)notification{
NSLog(@"willSleepNotification: received NSWorkspaceWillSleepNotification");
[self stopThread];
}
- (IBAction)startThreadAction:(id)sender
{
[self startThread];
}
- (IBAction)stopThreadAction:(id)sender
{
[self stopThread];
}
- (void)startThread
{
mRunThreadFlag = TRUE;
[NSThread detachNewThreadSelector: @selector(running:) toTarget:self withObject: nil];
}
- (void)stopThread
{
mRunThreadFlag = FALSE;
}
- (void)running:(id)object{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"running: enter++");
while( mRunThreadFlag )
{
mCounter++;
NSLog(@"running: counter is: %d, sleeping for 5 sec...", mCounter);
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 5]];
}
NSLog(@"running: exit--");
[pool release];
}
@end
Thanks!
Dragana
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden