2012-05-05 68 views
0

完成如何使backgroundthread不工作,直到另一个后台线程被完成,如何使其启动它的线程一旦第一backgroundthread被完成制作后台线程等待,直到其他后台线程获取iphone

+0

kindle请帮助我,如果你知道答案 – user1184202

+0

我已经写了一些代码使用它。对于更多的帮助,你应该在这里显示一些代码,所以我可以理解场景...享受.. – Nit

+0

可能需要使用串行调度队列。 https://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1 – user523234

回答

2

使用标志处理此类事件的类型,如下所示...

BOOL isYourthreadexecuting = NO; 

- (void)beginThread { 
    isYourthreadexecuting = YES; 

    [self performSelectorInBackground:@selector(backgroundThread) withObject:nil]; 
} 
- (void)backgroundThread { 
    [myClass performLongTask]; 

    // Done! 
    isYourthreadexecuting = NO; 
} 
- (void)waitForThread { 
    if (! isYourthreadexecuting) { 
     // Thread completed 
     [self callyourmethod]; 
    } 
} 

编辑>>加成根据使用评论

我建议你使用NSOperationQueue用于多线程。

希望,这将你...

+1

现在我正在遵循以下过程在你的答案中提到,但问题是第二个后台线程可能会开始任何时刻,它可能从其他类的方法也开始 – user1184202

+0

看到我编辑的答案.. – Nit

+1

@ user1184202如果你有你的答案,它会帮助你,然后标记它正确对其他人有帮助,对你也有帮助 – vishiphone

2

正如我在评论说,你可以使用GCD的串行调度队列。这里是一个示例代码来演示:

- (IBAction)buttonSerialQ2Pressed:(id)sender 
{ 
    dispatch_queue_t serialdQueue; 
    serialdQueue = dispatch_queue_create("com.mydomain.testbed.serialQ2", NULL); 
    dispatch_async(serialdQueue, ^{ 
     //your code here 
     [self method1]; 
    }); 
    dispatch_async(serialdQueue, ^{ 
     //your code here 
     [self method2]; 
    }); 
    dispatch_async(serialdQueue, ^{ 
     //your code here 
     [self method2]; 
    }); 
    dispatch_async(serialdQueue, ^{ 
     //your code here 
     [self method3]; 
    }); 
} 

-(void)method1 
{ 
    for (int i=0; i<1000; i++) 
    { 
     NSLog(@"method1 i: %i", i); 
    } 
} 

-(void)method2 
{ 
    for (int i=0; i<10; i++) 
    { 
     NSLog(@"method2 i: %i", i); 
    } 
} 

-(void)method3 
{ 
    for (int i=0; i<100; i++) 
    { 
     NSLog(@"method3 i: %i", i); 
    } 
}