gpt4 book ai didi

cocoa - NSTimer 永远不会启动

转载 作者:行者123 更新时间:2023-12-03 16:05:04 24 4
gpt4 key购买 nike

我只是想在几秒钟的延迟后关闭 NSPanel,但我无法启动我的 NSTimer。如果我显式调用它的 fire 方法,它就会触发,但它永远不会自行触发。这是我的代码:

   - (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]];

最佳答案

谢谢乔治!!

手动将计时器添加到运行循环中使其工作!

timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(removeProgress:) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

关于cocoa - NSTimer 永远不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4911804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com