Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 等待线程目标C中的值更改_Ios_Objective C_Multithreading_Nsthread - Fatal编程技术网

Ios 等待线程目标C中的值更改

Ios 等待线程目标C中的值更改,ios,objective-c,multithreading,nsthread,Ios,Objective C,Multithreading,Nsthread,我在目标C中有一个线程调用,我希望一旦这个线程结束,我希望返回一个值;该值将在线程内更改 因此,除非踏板结束,否则该方法不得返回该值 以下是我使用的代码: [NSThread detachNewThreadSelector:@selector(CheckBeforePrint2) toTarget:self withObject:nil]; 这是我的完整代码 - (NSString *) getStatusPrinter:(NSString *) printerSerialNumber {

我在目标C中有一个线程调用,我希望一旦这个线程结束,我希望返回一个值;该值将在线程内更改 因此,除非踏板结束,否则该方法不得返回该值

以下是我使用的代码:

[NSThread detachNewThreadSelector:@selector(CheckBeforePrint2) toTarget:self withObject:nil];
这是我的完整代码

- (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;
}
现在我想等待Result的值并返回我正在使用的结果

可可粉

我正在将值返回到另一个应用程序

有人能帮忙吗


谢谢

例如,您在这里所做的工作需要使用
信号灯。如果没有比这里提供的更多的东西,那么后台运行方法的完成块是最好的方法参见下面的选项2

无论哪种方式,您为什么希望父线程(调度新线程的线程)等待另一个线程?如果父线程正在等待,它将被锁定,直到调度的线程完成(根据您的要求)。这种情况是多余的,因为分派另一个线程的全部目的是让父线程可以继续处理其他事情。当然,除非父线程需要等待多个线程,否则锁定它是有意义的

话虽如此,最好让调度线程/父线程执行您正在调度到另一个线程的处理。我这么说只是考虑到你提供的细节

  • 选项1使用信号灯
使用信号量锁定和解锁父线程

-(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
}
但是,正如我前面提到的,这种情况似乎是多余的,因为父线程等待(因此不可用)子线程;为什么它不能自己做呢


  • 选项2使用完成块(首选)
使用传递给子线程的完成块。这允许父线程继续。如果它是主线程,就可以自由使用UI内容

-(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();
        }
    });
}
免责声明:此代码可能有拼写错误,因为它不是在编辑器/IDE中编写的。请评论,如果有的话


  • 更新以响应添加的详细信息
好的,因为您说需要将结果返回到另一个应用程序,这意味着在分派新线程后,不能允许
getStatusPrinter
处的输入线程返回。如果您确实需要为
CheckBeforePrint
创建一个新线程,那么输入线程必须等待。这对我来说毫无意义。您可以简单地在入口线程上运行所有内容

如果您使用的是
openURL:options:completionHandler:
,则条目线程不需要等待。
result
的值可以在完成块内传回


请参阅带有完成句柄的openURL上的

例如,您在这里所做的工作需要使用信号量。如果没有比这里提供的更多的东西,那么后台运行方法的完成块是最好的方法参见下面的选项2

无论哪种方式,您为什么希望父线程(调度新线程的线程)等待另一个线程?如果父线程正在等待,它将被锁定,直到调度的线程完成(根据您的要求)。这种情况是多余的,因为分派另一个线程的全部目的是让父线程可以继续处理其他事情。当然,除非父线程需要等待多个线程,否则锁定它是有意义的

话虽如此,最好让调度线程/父线程执行您正在调度到另一个线程的处理。我这么说只是考虑到你提供的细节

  • 选项1使用信号灯
使用信号量锁定和解锁父线程

-(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
}
但是,正如我前面提到的,这种情况似乎是多余的,因为父线程等待(因此不可用)子线程;为什么它不能自己做呢


  • 选项2使用完成块(首选)
使用传递给子线程的完成块。这允许父线程继续。如果它是主线程,就可以自由使用UI内容

-(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();
        }
    });
}
免责声明:此代码可能有拼写错误,因为它不是在编辑器/IDE中编写的。请评论,如果有的话


  • 更新以响应添加的详细信息
好的,因为您说需要将结果返回到另一个应用程序,这意味着在分派新线程后,不能允许
getStatusPrinter
处的输入线程返回。如果您确实需要为
CheckBeforePrint
创建一个新线程,那么输入线程必须等待。这对我来说毫无意义。您可以简单地在入口线程上运行所有内容

如果您使用的是
openURL:options:completionHandler:
,则条目线程不需要等待。
result
的值可以在完成块内传回


请参阅带有完成句柄的openURL上的

,我认为您可以使用委托来实现这一点。委托允许一个对象在事件发生时向另一个对象发送消息happens@JianZhong使用委托是可能的,但是多余的,因为方法
CheckBeforePrint2
在同一个类中。听起来绝对像是反模式。为了响应更新的代码,如果父线程仍要等待,则仍然不需要为
CheckBeforePrint
创建新线程。为什么您认为您需要在自己的线程中运行
CheckBeforePrint
?问题的最后一部分是将结果发送到另一个应用程序,这是另一个故事。您可以在一个单独的问题中发布这个问题(我确信这个挑战已经在堆栈上得到了回答,除非您已经回答了)