我只是想在几秒钟的时间内关闭NSPanel,但是我不能让我的NSTimer开始.如果我明确地称之为火焰方法,它会发射,但它本身永远不会消失.这是我的代码:
- (void)startRemoveProgresstimer:(NSNotification *)notification { NSLog(@"timer should start"); timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(removeProgress:) userInfo:nil repeats:NO]; } - (void)removeProgress:(NSTimer *)timer { [progresspanel close]; }
我的代码中有一些线程.我认为这是什么让我的计时器乱了
-(void)incomingTextUpdateThread:(NSThread*)parentThread { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //mark the thread as running readThreadRunning = TRUE; const int BUFFER_SIZE = 100; char byte_buffer[BUFFER_SIZE]; //buffer for holding incoming data int numBytes = 0; //number of bytes read Nsstring *text; //incoming text from the serial port [NSThread setThreadPriority:1.0]; //this will loop until the serial port closes while (TRUE) { //read() blocks until some data is available or the port is closed numBytes = read(serialFileDescriptor,byte_buffer,BUFFER_SIZE); if(numBytes > 0) { //creat a string from the incoming bytes text = [[[Nsstring alloc] initWithBytes:byte_buffer length:numBytes encoding:[Nsstring defaultCStringEncoding]] autorelease]; if(!([text rangeOfString:SEND_NEXT_COORDINATE].location == NSNotFound)) { //look for <next> to see if the next data should be sent if(coordinateNum <[coordinatesArray count]) { [self sendNextCoordinate]; //send coordinates } else { [self writeString:FINISH_COORDINATES_TRANSMIT]; //send <end> to mark transmission as complete NSNumber *total = [NSNumber numberWithUnsignedInteger:[coordinatesArray count]]; NSDictionary *userInfo = [NSDictionary dictionaryWithObject:total forKey:@"progress"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadProgressChange" object:self userInfo:userInfo]; //update progress bar to completed } } [self performSelectorOnMainThread:@selector(appendToIncomingText:) withObject:text waitUntilDone:YES]; //write incoming text to NSTextView } else { break; //Stop the thread if there is an error } } // make sure the serial port is closed if (serialFileDescriptor != -1) { close(serialFileDescriptor); serialFileDescriptor = -1; } // mark that the thread has quit readThreadRunning = FALSE; // give back the pool [pool release]; }
其他方法通过以下方式调用:[self performSelectorInBackground:@selector(incomingTextUpdateThread
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。