使用xcode中的线程调用函数 [英] call a function using thread in xcode

查看:354
本文介绍了使用xcode中的线程调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在xcode中创建了一个线程,我已经给出了从该线程调用的函数名称。
但我的问题是没有调用给调用的函数名称(在该函数中放置断点时才知道)

i have created a thread in xcode and i have given the function name to be called from that thread. but my problem is that the function name which is given to call is not being called(came to know when put a breakpoint in that function)

代码:

 NSThread* myThread; 
 [myThread start]; 
 [self performSelector:@selector(func1:) onThread:myThread withObject:nil waitUntilDone:false]

后来我也尝试了这个:

NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(func1:)object:nil];
[myThread start]; 

以上func1是要调用的函数的名称。

above func1 is the name of the function to be called.

所以任何人都可以告诉我如何创建线程并从那里调用func1 ....

so can any one please tell me how to create the thread and call func1 from there....

推荐答案

在您的第一个代码示例中,它看起来并不像您实际上正在创建新线程。您创建一个空的 myThread 变量,然后在其上调用 start ,但这只会导致开始发送到 nil 。然后将空线程变量发送到 performSelector:onThread:withObject:waitUntilDone:方法,该方法可能无效。

In your first code sample it doesn't look like you are actually creating a new thread. You create an empty myThread variable and then call start on it but this will just result in start being sent to nil. The empty thread variable is then sent to the performSelector:onThread:withObject:waitUntilDone: method which will presumably do nothing.

在使用 performSelector:onThread:withObject:waitUntilDone:

或者,假设您不关心该方法运行的后台线程,只需使用就会容易得多performSelectorInBackground:withObject:。例如:

Alternatively, it would be much easier, assuming you don't care which background thread the method runs on, to simply use performSelectorInBackground:withObject:. For example:

[self performSelectorInBackground:@selector(func1:) withObject:nil];

这篇关于使用xcode中的线程调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆