NSTask keeps running - how to stop?
NSTask keeps running - how to stop?
- Subject: NSTask keeps running - how to stop?
- From: Stephan Ruggiero <email@hidden>
- Date: Tue, 15 Mar 2005 09:03:14 +0100
Hello list,
I am quite new to cocoa development and currently writing a little tool to control some shell scripts from a GUI. Everything works just fine, but I encountered a problem I do not find a solution to:
I want the user to be able to monitor six different log files (only one at a time), so I placed six TextViews in a TabView. Each time a tab is selected, I call an NSTask to start "tail -f -n 100" with the appropriate file name. It should provied "Console.app"-like functionality.
To avoid having the GUI blocked I start a new thread to listen to the tail task and write new data to the appropriate TextView.
This is an extract of the code I use:
-(void)loadLogData:(NSString *)logToDisplay { //logToDisplay is given depending on the current TabViewItem's identifier
NSString *pathToLog = [....] // I give the path here
NSTask *tail=[[NSTask alloc] init];
NSPipe *pipe=[[NSPipe alloc] init];
NSFileHandle *handle;
int linesToDisplay=100;
[tail setLaunchPath:@"/usr/bin/tail"];
[tail setArguments:[NSArray arrayWithObjects:@"-n"stringByAppendingString:[NSString stringWithFormat:@" %i",linesToDisplay]],pathToLog,nil]];
[tail setStandardOutput:pipe];
handle=[pipe fileHandleForReading];
[tail launch];
[NSThread detachNewThreadSelector:@selector(copyData:) toTarget:self withObject:handle];
[pipe release];
[tail release];
}
- (void)copyData:(NSFileHandle *)handle
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSData *data;
while ([data=[handle availableData] length]) {
NSString *logTxt=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSRange theEnd=NSMakeRange([[txtView string] length],0);
[txtView replaceCharactersInRange:theEnd withString:logTxt];
theEnd.location+=[logTxt length];
[txtView scrollRangeToVisible:theEnd];
[logTxt release];
}
[pool release];
}
This works fine, but my problem is that I do not know how to terminate the NSTask (tail) - if the user switches to an other tab, a second "tail" is started, then a third one... I had 20 "tails" running around here :-)
For now I implemented a BOOL variable to know if a "tail" was already started and if so I call a shell script that determines the PID and kills the process - quite a dirty solution and it causes the GUI to react slower.
Do you know some other way to terminate the "tail -f" NSTask from cocoa?
(I tried to call [tail terminate] right before [pipe release] to see if tail is stopped, but it keeps running...)
Thank you very much for your help!
Stephan _______________________________________________
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