bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSTask or NSNotification problem


From: Matt Gundry
Subject: NSTask or NSNotification problem
Date: Wed, 20 Jun 2001 11:31:15 -0500

NOTE: Duplicate filed with GNUStep bug report database.

I am having a problem with recieving notifications from tasks. I'm not
sure if this is an NSTask issue, a problem with NSNotification or just
confusion on my part.

I need to do a number of tasks in order, so I've created a task list and
launch the first task. I then listen for NSTaskDidTerminateNotification.
When that notification arrives, I remove the completed task from the
task list and launch the next one. The problem is that I am not
receiving the notifications unless my runloop does some busy work (eg.
NSLog).

I am currently using GNUStep Base 1.0.2 on Redhat 7.0 with the newer Gcc
2.96-81.

I have created a small test program that demonstrates the problem.
Remove the comment in front of NSLog and the program will operate the
way I expect.

Thanks,
Matt Gundry
mjgundry@faa-engineers.com

--CUT-HERE----CUT-HERE----CUT-HERE----CUT-HERE----CUT-HERE----CUT-HERE--

#import <Foundation/Foundation.h>

@interface TaskMan : NSObject
{
        NSMutableArray *taskList;
}

-nextTask:(NSNotification *) aNotification;
@end

@implementation TaskMan
-init
{
        NSTask *aTask;

        self = [super init];

        [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(nextTask:)
                name:NSTaskDidTerminateNotification
                object:nil];

        taskList = [[NSMutableArray alloc] init];

        aTask = [[NSTask alloc] init];
        [aTask setLaunchPath:@"/bin/ls"];
        [aTask setArguments:nil];
        [taskList addObject:aTask];

        aTask = [[NSTask alloc] init];
        [aTask setLaunchPath:@"/bin/ps"];
        [aTask setArguments:nil];
        [taskList addObject:aTask];

        aTask = [[NSTask alloc] init];
        [aTask setLaunchPath:@"/bin/pwd"];
        [aTask setArguments:nil];
        [taskList addObject:aTask];

        aTask = [[NSTask alloc] init];
        [aTask setLaunchPath:@"/bin/date"];
        [aTask setArguments:nil];
        [taskList addObject:aTask];

        [[taskList objectAtIndex:0] launch];

        return self;
}

-nextTask:(NSNotification *) aNotification
{
        if ([[aNotification object] terminationStatus] == 0) {
                [NSNotification 
notificationWithName:@"CommandCompletedSuccessfully"
                        object:self];
        } else {
                [NSNotification notificationWithName:@"CommandFailed"
                        object:self];
        }
        [taskList removeObjectAtIndex:0];

        if ([taskList count] > 0)
                [[taskList objectAtIndex:0] launch];
        else
                exit(0);

        return self;
}
@end

int main(int argc, char **argv, char** env)
{
        NSAutoreleasePool *pool;
        TaskMan *aTaskMan;

        pool = [NSAutoreleasePool new];
        aTaskMan = [[TaskMan alloc] init];

        while(1) {
                [[NSRunLoop currentRunLoop] runOnceBeforeDate:
                        [NSDate dateWithTimeIntervalSinceNow: 1]];

/* Uncomment the following line, and the app will complete all tasks */
/* otherwise it will hang */
//      NSLog(@"");
        }

        exit(0);        
}
-- 
Matt Gundry, EIT, Project Engineer

At Work         mjgundry@faa-engineers.com
Web             http://www.faa-engineers.com/~mjgundry/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]