简体   繁体   中英

Wait for value to change in thread Objective C

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 semaphore for 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 below

Either 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.

  • OPTION 1 use a semaphore

Use a semaphore to lock and unlock parent thread

-(void)getStatusPrinter()
{   
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [NSThread detachNewThreadSelector:@selector(checkBeforePrint2) toTarget:self withObject: semaphore];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    [self print]; // this will run after semaphore is unlocked
}

-(void)checkBeforePrint2:(dispatch_semaphore_t)sem
{
    //this is within child thread
    //do some processing,
    dispatch_semaphore_signal(sem);//unlock semaphore
}

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...


  • OPTION 2 use completion block (PREFERABLE)

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.

-(void)getStatusPrinter()
{   
    [self checkBeforePrint2WithCompletion: ^{
        [self print];
    }];
    //continue with more stuff
}

-(void)checkBeforePrint2WithCompletion:(void (^ __nullable)(void))completion
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //do something before executing completion code

        if(completion){
            completion();
        }
    });
}

Disclaimer: there may be typos in this code as it was not written in an editor/ IDE. Please comment if any.


  • UPDATE in response to added details.

Okay, for the fact that you said you need to return the result to another application, that means the entry thread at getStatusPrinter can not be allowed to return after dispatching a new thread. If you really need to create a new thread for CheckBeforePrint then 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 of result can be passed back within the completion block.

Please refer to Apple's documentation on openURL with a completion handle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM