可可 – NSTimer从未开始

前端之家收集整理的这篇文章主要介绍了可可 – NSTimer从未开始前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只是想在几秒钟的时间内关闭NSPanel,但是我不能让我的NSTimer开始.如果我明确地称之为火焰方法,它会发射,但它本身永远不会消失.这是我的代码
  1. - (void)startRemoveProgressTimer:(NSNotification *)notification {
  2. NSLog(@"timer should start");
  3. timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(removeProgress:) userInfo:nil repeats:NO];
  4. }
  5.  
  6. - (void)removeProgress:(NSTimer *)timer {
  7. [progressPanel close];
  8. }

我的代码中有一些线程.我认为这是什么让我的计时器乱了

  1. -(void)incomingTextUpdateThread:(NSThread*)parentThread {
  2. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  3.  
  4. //mark the thread as running
  5. readThreadRunning = TRUE;
  6.  
  7. const int BUFFER_SIZE = 100;
  8. char byte_buffer[BUFFER_SIZE]; //buffer for holding incoming data
  9. int numBytes = 0; //number of bytes read
  10. NSString *text; //incoming text from the serial port
  11.  
  12. [NSThread setThreadPriority:1.0];
  13.  
  14. //this will loop until the serial port closes
  15. while (TRUE) {
  16. //read() blocks until some data is available or the port is closed
  17. numBytes = read(serialFileDescriptor,byte_buffer,BUFFER_SIZE);
  18. if(numBytes > 0) {
  19. //creat a string from the incoming bytes
  20. text = [[[NSString alloc] initWithBytes:byte_buffer length:numBytes encoding:[NSString defaultCStringEncoding]] autorelease];
  21. if(!([text rangeOfString:SEND_NEXT_COORDINATE].location == NSNotFound)) {
  22. //look for <next> to see if the next data should be sent
  23. if(coordinateNum <[coordinatesArray count]) {
  24. [self sendNextCoordinate]; //send coordinates
  25. }
  26. else {
  27. [self writeString:FINISH_COORDINATES_TRANSMIT]; //send <end> to mark transmission as complete
  28. NSNumber *total = [NSNumber numberWithUnsignedInteger:[coordinatesArray count]];
  29. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:total forKey:@"progress"];
  30. [[NSNotificationCenter defaultCenter] postNotificationName:@"uploadProgressChange" object:self userInfo:userInfo]; //update progress bar to completed
  31. }
  32.  
  33.  
  34. }
  35.  
  36. [self performSelectorOnMainThread:@selector(appendToIncomingText:) withObject:text waitUntilDone:YES]; //write incoming text to NSTextView
  37. } else {
  38. break; //Stop the thread if there is an error
  39. }
  40. }
  41.  
  42. // make sure the serial port is closed
  43. if (serialFileDescriptor != -1) {
  44. close(serialFileDescriptor);
  45. serialFileDescriptor = -1;
  46. }
  47.  
  48. // mark that the thread has quit
  49. readThreadRunning = FALSE;
  50.  
  51. // give back the pool
  52. [pool release];
  53. }

其他方法通过以下方式调用:[self performSelectorInBackground:@selector(incomingTextUpdateThread

猜你在找的iOS相关文章