Re: Wait for NSTask to finish
Re: Wait for NSTask to finish
- Subject: Re: Wait for NSTask to finish
- From: Hamish Allan <email@hidden>
- Date: Wed, 16 Mar 2005 15:48:32 +0000
Hi,
Is there a way to wait for an NSTask (or a method I created) to finish
before continuing like waitUntilExit, but doesn't cause my app to
hang/become unresponsive?
Thanks
You could launch the NSTask on a seperate thread. This way it won't
tie up the interface. If you launch the thread on the main event
thread (the thread the interface is running on) then you want your app
to become unresponsive until the thread is done so as not to "upset"
the task you're running.
If you take this approach, you may also find NSObject's
performSelectorOnMainThread: methods useful. e.g.,
// The following code is composed in Mail.app, usual disclaimers apply:
- (IBAction)buttonPressed:(id)sender
{
NSNumber *returnValue = [NSNumber alloc];
[NSThread detachNewThreadSelector: @selector(doButtonPressedTask:)
toTarget:self withObject:returnValue];
}
- (void)doButtonPressedTask:(id)returnValue
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/bin/ls"
arguments:nil];
[task waitUntilExit];
[self
performSelectorOnMainThread:@selector(buttonPressedTaskDidFinish:)
withObject:[returnValue initWithInt:[task terminationStatus]]
waitUntilDone:NO];
[pool release];
}
- (void)buttonPressedTaskDidFinish:(id)returnValue
{
int retVal = [returnValue intValue];
[returnValue release];
}
The use of alloc with delayed init is a bit unorthodox, perhaps someone
else can think of a better way to deal with the issue of object
ownership?
Best wishes,
Hamish
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden