Test running two tasks at once

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@9620 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2001-04-19 08:31:19 +00:00
parent aa10e2f2f9
commit db38292b80

View file

@ -1,16 +1,32 @@
#include <Foundation/NSAutoreleasePool.h> #include <Foundation/Foundation.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSTask.h>
@interface TaskObs : NSObject
- (void) terminated: (NSNotification*)aNotification;
@end
@implementation TaskObs
- (void) terminated: (NSNotification*)aNotification
{
NSLog(@"Task (%@) terminated", [aNotification object]);
}
@end
int int
main() main()
{ {
id pool; NSAutoreleasePool *pool;
NSDictionary *env; NSDictionary *env;
NSTask *task; NSTask *task;
NSTask *t0, *t1;
NSData *d; NSData *d;
TaskObs *obs = [TaskObs new];
pool = [[NSAutoreleasePool alloc] init]; pool = [NSAutoreleasePool new];
[[NSNotificationCenter defaultCenter]
addObserver: obs
selector: @selector(terminated:)
name: NSTaskDidTerminateNotification
object: nil];
#ifdef __MINGW__ #ifdef __MINGW__
task = [NSTask launchedTaskWithLaunchPath: @"C:\\WINDOWS\\COMMAND\\MEM.EXE" task = [NSTask launchedTaskWithLaunchPath: @"C:\\WINDOWS\\COMMAND\\MEM.EXE"
@ -22,8 +38,8 @@ main()
[task waitUntilExit]; [task waitUntilExit];
printf("Exit status - %d\n", [task terminationStatus]); printf("Exit status - %d\n", [task terminationStatus]);
[pool release]; RELEASE(pool);
pool = [[NSAutoreleasePool alloc] init]; pool = [NSAutoreleasePool new];
task = [NSTask new]; task = [NSTask new];
env = [[[[NSProcessInfo processInfo] environment] mutableCopy] autorelease]; env = [[[[NSProcessInfo processInfo] environment] mutableCopy] autorelease];
@ -37,8 +53,28 @@ main()
NSLog(@"Got PATH of '%*s'", [d length], [d bytes]); NSLog(@"Got PATH of '%*s'", [d length], [d bytes]);
[task waitUntilExit]; [task waitUntilExit];
[task release]; RELEASE(task);
[pool release];
NSLog(@"Testing two tasks at the same time");
t0 = [NSTask launchedTaskWithLaunchPath: @"/bin/sh"
arguments:
[NSArray arrayWithObjects: @"-c", @"echo task0", nil]];
NSLog(@"Launched task0 - %@", t0);
t1 = [NSTask launchedTaskWithLaunchPath: @"/bin/sh"
arguments:
[NSArray arrayWithObjects: @"-c", @"echo task1", nil]];
NSLog(@"Launched task1 - %@", t1);
while ([t0 isRunning] == YES || [t1 isRunning] == YES)
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
[[NSRunLoop currentRunLoop] runOnceBeforeDate:
[NSDate dateWithTimeIntervalSinceNow: 1]];
RELEASE(arp);
}
RELEASE(pool);
exit(0); exit(0);
} }