在尝试了多种方法在新线程中调用函数之后,只有下面的代码对我有用
[NSThread detacNewThreadSelector:@selector(temp:) toTarget:self withObject:self];
以下没有工作:
NSThread *updateThread1 = [[NSThread alloc] initWithTarget:self selector:@selector(temp:) object:self];
NSThread *updateThread1 = [[NSThread alloc] init];
[self performSelector:@selector(temp:) onThread:updateThread1 withObject:self waitUntilDone:YES];
现在,当我尝试NSTimer
在函数中调用或执行选择器时timer:
它不起作用在代码下方查找
int timeOutflag1 = 0;
-(void)connectCheckTimeOut
{
NSLog(@"timeout");
timeOutflag1 = 1;
}
-(void)temp:(id)selfptr
{
//[selfptr connectCheckTimeOut];
NSLog(@"temp");
//[NSTimer scheduledTimerWithTimeInterval:5 target:selfptr selector:@selector(connectCheckTimeOut) userInfo:nil repeats:NO];
[selfptr performSelector:@selector(connectCheckTimeOut) withObject:nil afterDelay:5];
}
- (IBAction)onUart:(id)sender {
protocolDemo1 *prtDemo = [[protocolDemo1 alloc] init];
//NSThread *updateThread1 = [[NSThread alloc] initWithTarget:self selector:@selector(temp:) object:self];
//[self performSelector:@selector(temp:) onThread:updateThread1 withObject:self waitUntilDone:YES];
// [updateThread1 start];
[self performSelector:@selector(temp:) withObject:self afterDelay:0];
while(1)
{
NSLog(@"Whieloop");
if(timeOutflag1)
{
timeOutflag1 = 0;
break;
}
if([prtDemo isConnected])
break;
}
}
如果我[self performSelector:@selector(connectCheckTimeOut) withObject:nil afterDelay:5];
在onUart
函数中使用,那么它可以正常工作,我可以看到Timeout
printf
但在 temp 内部它不起作用。