performSelector:onThread会破坏运行循环吗? [英] performSelector:onThread breaks runloop?

查看:331
本文介绍了performSelector:onThread会破坏运行循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何使用API​​ performSelector:onThread,在这里我需要一些建议.
据我所知,我需要一个runloop来调用performSelector:onThread,所以我做了一个.但是随后我发现了一个问题:呼叫performSelector:onThread后,runloop就会停止.

I'm not sure how to use the API performSelector:onThread and I need some suggestions here.
As far as I known, I need a runloop to make the call of performSelector:onThread, so I made one. But then I find a problem : once I called performSelector:onThread, the runloop stops.

这是我的测试代码,runloop在function kickOffThread中.

Here 's my test code, runloop is in function kickOffThread.

- (void)setCurrentThread
{
    self.thread = [NSThread currentThread];
}

- (void)kickOffThread
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self performSelector:@selector(setCurrentThread) withObject:nil afterDelay:0];
    while (!threadCheck) {
        NSLog(@"threadCheck = %d", threadCheck);
        [[NSRunLoop currentRunLoop] run];
    }
    [self doSomeCleanUp];
    NSLog(@"thread exit ....");
    [pool release];
}

我通过performInBackground呼叫kickOffThread:

   [self performSelectorInBackground:@selector(kickOffThread) withObject:nil];

在mainThread中,我调用以下API将threadCheck设置为YES,它被正确调用,但是我的线程循环突然停止.

In mainThread I call following API to set threadCheck to be YES, it is correctly called, but my thread loop suddenly stops.

[self performSelector:@selector(signalThreadCheck) onThread:self.thread withObject:nil waitUntilDone:NO];
-(void)signalThreadCheck
{
    NSLog(@" signalThreadCheck ... ");
    threadCheck = YES;
}

终端日志结果如下,未打印线程退出....".有人告诉我问题出在哪里吗?

terminal log result is as follows, "thread exit ...." not printed. Anyone tells me where is the problem ?

2013-06-07 15:51:54.827 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.827 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.827 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.836 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.840 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.844 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.846 MBSMapSample[23582:17403]  signalThreadCheck ... 

推荐答案

线程未中断. 它只是卡在了mach_msg_trap

the thread is not breaked. it just stucked in mach_msg_trap

如果没有输入源或计时器附加到运行循环,则此方法立即退出;否则,此方法将退出.否则,它将在NSDefaultRunLoopMode中运行接收器

If no input sources or timers are attached to the run loop, this method exits immediately; otherwise, it runs the receiver in the NSDefaultRunLoopMode

perfromSelector:onThread导致调用方将输入源添加到目标线程. 因此[NSRunLoop run]方法实际上将调用[NSRunLoop(NSRunLoop)runMode:beforeDate:] 并发送一个无限的日期作为beforeDate参数.

the perfromSelector:onThread cause the caller add a input source to the target thread. so the method [NSRunLoop run] will actually call [NSRunLoop(NSRunLoop) runMode:beforeDate:] and send a infinite date as beforeDate param.

如果要停止线程,请改用[NSRunLoop(NSRunLoop)runMode:beforeDate:],并发送一个有限的日期,例如:

if you want to stop the thread, use [NSRunLoop(NSRunLoop) runMode:beforeDate:] instead, and send a limited date, like this:

BOOL shouldKeepRunning = YES;        // global
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
while (shouldKeepRunning && 
    [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

这篇关于performSelector:onThread会破坏运行循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆