user908141
user908141

Reputation: 11

How to start NSThread after finishing other NSThread?

I have a couple threads. I want to run them making sure they are executing one after another. [run thread1]; [run thread2]; When I do this, thread2 is running without waiting thread1 to be finished. I need this because I need value from thread 1, to use on thread 2.

Upvotes: 0

Views: 1073

Answers (1)

Davyd Geyl
Davyd Geyl

Reputation: 4623

If you really need threads, for whatever reason, you can do it this way.

- (void)startThreadOne
{
    [NSThread detachNewThreadSelector:@selector(executeThreadOne) toTarget:self withObject:nil];
}

- (void)executeThreadOne
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    // calculate the value
    [self performSelectorOnMainThread:@selector(threadOneDidFinish) withObject:nil waitUntilDone:NO];
    [pool release];
}

- (void)threadOneDidFinish
{
    [NSThread detachNewThreadSelector:@selector(executeThreadTwo) toTarget:self withObject:nil];
}

However, this way may be considered old fashioned. Serial dispatch queues should be used when it is possible. Concurrency Programming Guide.

Upvotes: 1

Related Questions