NSTimer 永远不会启动

发布于 2024-10-16 05:34:20 字数 2726 浏览 6 评论 0原文

我只是想在几秒钟的延迟后关闭 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]];

I'm just trying to close an NSPanel after a couple second delay, but I can't get my NSTimer to start. It will fire if I explicitly call the fire method on it, but it will never go by itself. Here's my code:

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

I do have some threading in my code as such. I assume this is what's messing my timer up.

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

Which is called from another method by: [self performSelectorInBackground:@selector(incomingTextUpdateThread:) withObject:[NSThread currentThread]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2024-10-23 05:34:20

谢谢乔治!!

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

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

Thank you rgeorge!!

Adding the timer to the run loop manually made it work!

timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(removeProgress:) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文