我有一个非常简单的程序,它每 2 秒运行一次脚本并将输出放入菜单栏项。但是,大约 90 分钟后,菜单栏项目停止更新。谁能明白为什么?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[_window setIsVisible:NO];
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
//the above code stops the dock icon from appearing (and also stops the application from appearing in the alt tab menu
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setHighlightMode:YES];
[statusItem setTitle:@"0%"];
[statusItem setEnabled:YES];
[statusItem setToolTip:@"Script output"];
//[statusItem setAction:@selector(updateIPAddress:)];
[statusItem setTarget:self];
//NSString *output = [self unixSinglePathCommandWithReturn:@"/usr/bin/osascript \"/Users/Velox/Projects/Geektool Scripts/WorkDay.scpt\""];
[NSThread detachNewThreadSelector:@selector(calcPercent) toTarget:[self class] withObject:nil];
}
+(void)calcPercent{
while(true){
NSString *output = [self unixSinglePathCommandWithReturn:@"/usr/bin/osascript \"/Users/Velox/Projects/Geektool Scripts/WorkDay.scpt\""];
[statusItem setTitle:output];
[statusItem setEnabled:YES];
[NSThread sleepForTimeInterval:2];
}
}
+ (NSString *)unixSinglePathCommandWithReturn:(NSString *) command {
NSPipe *newPipe = [NSPipe pipe];
NSFileHandle *readHandle = [newPipe fileHandleForReading];
NSData *inData = nil;
NSString* returnValue = nil;
NSTask *unixTask = [[NSTask alloc] init];
unixTask = [[NSTask alloc] init];
[unixTask setStandardOutput:newPipe];
[unixTask setLaunchPath:@"/bin/csh"];
[unixTask setArguments:[NSArray arrayWithObjects:@"-c", command , nil]];
[unixTask launch];
[unixTask waitUntilExit];
//int status = [unixTask terminationStatus];
while ((inData = [readHandle availableData]) && [inData length]) {
returnValue= [[NSString alloc] initWithData:inData encoding:[NSString defaultCStringEncoding]];
returnValue = [returnValue substringToIndex:[returnValue length]-1];
}
return returnValue;
}
我正在使用 ARC。
谢谢。