Я просто пытаюсь закрыть NSPanel после пары секундной задержки, но я не могу запустить NSTimer. Он будет срабатывать, если я прямо назову метод огня на нем, но он никогда не пройдет сам по себе. Вот мой код: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:) withObject:[NSThread currentThread]];
Хм ... не вижу ничего плохого, но почему вы передаете currentThread как параметр withObject:? Кажется, вы не используете его в этом методе, почему бы просто не пропустить ноль? – jakev
У меня есть подозрение (просто подозрение, так как я не вижу, что вызывает startRemoveProgressTimer :) scheduleTimerWithTimeInterval добавляет таймер к циклу запуска текущего потока, а не к основному потоку. Если вы создаете свой таймер в фоновом потоке, который никогда не использует свой цикл запуска - если он просто сидит и спин за какое-то время (1) считывает цикл из файлового дескриптора, скажем - цикл цикла никогда не получает возможности справиться с любые таймеры. Попробуйте явно добавить его в цикл запуска основного потока и посмотреть, что произойдет. – rgeorge
Будет ли это иметь значение, если я сказал вам, что startRemoveProgressTimer: привязан к NSNotification? Что касается того, почему я прохожу в текущей теме, я не уверен. Я пытаюсь адаптировать чей-то код к моему приложению. Исходный код находится здесь: http://arduino.cc/playground/Interfacing/Cocoa, но мне пришлось внести некоторые изменения, чтобы заставить его скомпилировать (Snow) Leopard, и я ввел некоторые из своих собственных логик. –