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
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today