-(void)startThread {
m_bRunThread = YES;
if(m_bRunThread) {
NSThread* myThread = [[NSThread alloc]initWithTarget:self selector:@selector(display) object:theConditionLock];
[myThread start];
/*((WaitForSingleobject(event,1500) != WAIT_OBJECT_O) || !m_bRunThread) {
m_bRunThread = false;
Trace(_T("Unable to start display Thread\n"));
}*/
}
[self insert];
}
-(void)insert {
[theConditionLock lockWhenCondition:LOCK];
NSLog(@"I'm into insert function of thread!");
[theConditionLock unlockWithCondition:UNLOCK];
}
-(void)display {
NSLog(@"I'm into display function");
while (YES) {
[theConditionLock lockWhenCondition:LOCK];
NSAutoreleasePool* pool1 = [[NSAutoreleasePool alloc]init];
NSLog(@"Into the lock");
[theConditionLock unlockWithCondition:UNLOCK];
[pool1 drain];
}
}
在调用insert方法之前调用startThread.display调用insert和display方法。但是我希望显示等到插入完成它的执行。如果它停止了一个信号必须是发送到开始线程以显示错误消息。
怎么做。
但是在上面的代码中,首先调用display方法,然后在无限循环中继续。
答案 0 :(得分:0)
如果你想在显示前调用insert,只需将它移到调用上方即可启动线程。
m_bRunThread = YES;
[self insert];
if(m_bRunThread)
{
NSThread* myThread = [[NSThread alloc]initWithTarget:self selector:@selector(display) object:theConditionLock];
[myThread start];
}
答案 1 :(得分:0)
您可以在performSelector:onThread:withObject:waitUntilDone:
self
根据我的理解,你想同步插入和显示线程,这样在insert(startThread)之后调用display,并报告回insert(startThread)(不确定我是否正确)。 如果我把它弄好,那么这样的事情应该可以解决问题(没有经过测试,可能需要一些小改动):
[self insert];
NSThread* myThread = [[NSThread alloc] init];
[self performSelector:@selector(display) onThread:myThread withObject:nil waitUntilDone:YES];
//myThread stopped, check its return status (maybe set some variables) and display error message
这将是另一种方式:
m_bRunThread = YES;
[self insert];
if(m_bRunThread)
{
NSThread* myThread = [[NSThread alloc]initWithTarget:self selector:@selector(display) object:theConditionLock];
[myThread start];
}
while(m_bRunThread){//check if display is still running
[NSThread sleepForTimeInterval:0.5];
}
//display thread stopped