RE: Question on threads running in my Foundation tool
RE: Question on threads running in my Foundation tool
- Subject: RE: Question on threads running in my Foundation tool
- From: "Charles E. Heizer" <email@hidden>
- Date: Sun, 22 Feb 2009 09:35:35 -0800
- Acceptlanguage: en-US
- Thread-topic: Question on threads running in my Foundation tool
OK,
I reworked my test app so that I could isolate the threads and to see what's going on a little better, and I'm now using a NSTimer and a NSRunLoop. The issue I'm seeing is that for every time I call NSThread so not to block the run loop it runs it's course but when it completes it leaves a hanging thread. So every 60 seconds I see an additional thread get added and never drops it once completed.
I'm including my source, can someone please show me how to make sure the thread ends when the task is completed.
thanks,
- Charles
#import <Foundation/Foundation.h>
@interface MYTestObject : NSObject {
id myObjData;
}
- (void) sayHello;
- (void) runSoftwareUpdate;
- (void) runSWThread:(id)myObjData;
- (void) setupSWInSeperateThread;
@end
@implementation MYTestObject
- (void)sayHello
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello again...");
[pool release];
}
-(void)runSoftwareUpdate
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/softwareupdate"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-l", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"Got a result \n%@", string);
}
- (void) runSWThread:(id)myObjData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self runSoftwareUpdate];
[pool release];
}
- (void) setupSWInSeperateThread
{
[NSThread detachNewThreadSelector:@selector(runSWThread:) toTarget:self withObject:myObjData];
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[NSTimer scheduledTimerWithTimeInterval:2 //Say Hello every 2 seconds
target:[[[MYTestObject alloc] init] autorelease]
selector:@selector(sayHello)
userInfo:nil
repeats:TRUE];
[NSTimer scheduledTimerWithTimeInterval:60 // Run my NSTask that I know spawns a few other threads
target:[[[MYTestObject alloc] init] autorelease]
selector:@selector(setupSWInSeperateThread)
userInfo:nil
repeats:TRUE];
[[NSRunLoop currentRunLoop] run];
[pool release];
return 0;
} _______________________________________________
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