I have a thread call in objective C and i want once this thread ends i want to return a value ;the value will be changed inside the thread So the method must not return the value unless the tread ends
Here is the code i use:
[NSThread detachNewThreadSelector:@selector(CheckBeforePrint2) toTarget:self withObject:nil];
This is My Full Code
- (NSString *) getStatusPrinter:(NSString *) printerSerialNumber
{
self.printerSN = printerSerialNumber;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *Result = @"-1";
[NSThread sleepForTimeInterval:2.0f];
[NSThread detachNewThreadSelector:@selector(CheckBeforePrint) toTarget:self withObject:Result];
[pool release];
return Result;
}
Now i want to wait for the value of Result and return it i am using
cocoa
And i am returning the value to another app
Can anyone help in that.
Thanks
What you are doing here requires the use of a
semaphorefor example. If there is nothing more to it than you are providing here then a completion block to a background running method is the best way to do it. See option 2 belowEither way, why do you want the parent thread (the thread dispatching a new thread) to wait for another thread? If the parent thread is waiting, it is locked until the dispatched thread is done (according to your requirement). This situation is redundant because the whole point of dispatching another thread is so that the parent thread can continue with other things. Unless of course the parent thread needs to wait for multiple threads, then it makes sense to lock it.
Having said that, its best to just let the dispatching thread / parent thread do the processing that you are dispatching on to another thread. Im only saying this given the details you have provided.
Use a semaphore to lock and unlock parent thread
But, as I mentioned before, this situation seems redundant because the parent thread waits (therefore unusable) for child thread; why can't it just do the work itself...
Use a completion block that you pass to the child thread. This allows the parent thread to continue. If it is the main thread it remains free for UI stuff.
Disclaimer: there may be typos in this code as it was not written in an editor/ IDE. Please comment if any.
Okay, for the fact that you said you need to return the result to another application, that means the entry thread at
getStatusPrintercan not be allowed to return after dispatching a new thread. If you really need to create a new thread forCheckBeforePrintthen the entry thread has to wait. That to me is pointless. You can simply run everything on the entry thread.If you are using
openURL:options:completionHandler:then the entry thread doesn't need to wait. The value ofresultcan be passed back within the completion block.Please refer to Apple's documentation on openURL with a completion handle