• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

performSelector:onThread会破坏运行循环吗?

用户头像
it1352
帮助1

问题说明

我不确定如何使用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 ... 

正确答案

#1

线程未中断. 它只是卡在了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]]);

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhacj
系列文章
更多 icon
同类精品
更多 icon
继续加载