在 xcode 中使用线程调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10427140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
call a function using thread in xcode
提问by Subrat nayak.
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)
我在 xcode 中创建了一个线程,并给出了要从该线程调用的函数名称。但我的问题是没有调用给调用的函数名(当在该函数中放置断点时才知道)
code:
代码:
NSThread* myThread;
[myThread start];
[self performSelector:@selector(func1:) onThread:myThread withObject:nil waitUntilDone:false]
and later i tried this one also:
后来我也尝试了这个:
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(func1:)object:nil];
[myThread start];
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....
那么任何人都可以告诉我如何创建线程并从那里调用 func1 ....
回答by mttrb
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.
在您的第一个代码示例中,您实际上并没有创建一个新线程。您创建一个空myThread
变量,然后调用start
它,但这只会导致start
被发送到nil
. 然后将空线程变量发送到performSelector:onThread:withObject:waitUntilDone:
可能什么都不做的方法。
You will need to properly create a thread before you can actually run something on it using performSelector:onThread:withObject:waitUntilDone:
.
您需要正确创建一个线程,然后才能使用performSelector:onThread:withObject:waitUntilDone:
.
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:
或者,假设您不关心该方法在哪个后台线程上运行,只需使用performSelectorInBackground:withObject:
. 例如:
[self performSelectorInBackground:@selector(func1:) withObject:nil];
回答by Rui Peres
Try the following if it works:
如果有效,请尝试以下操作:
[NSThread detachNewThreadSelector:@selector(func1) toTarget:self withObject:nil];
Since you are not passing any object to your "func1" (aka: your method doesn't have parameters) you don't need to put the ":" after its name.
由于您没有将任何对象传递给您的“func1”(又名:您的方法没有参数),因此您无需在其名称后放置“:”。
回答by iDevAmit
If your func1 accepting one argument. Then definitely it has to work with second approach which you used. May be your fuc1 has no formal argument and still u calling in selector like this @selector(fuc1:) and passing object as a nil. so may be due to this reason it is not working. It can be one reason. just try it if not.
如果您的 func1 接受一个参数。那么肯定它必须与您使用的第二种方法一起使用。可能是你的 fuc1 没有正式的参数,你仍然像这样调用选择器 @selector(fuc1:) 并将对象作为 nil 传递。所以可能是由于这个原因它不工作。这可能是一个原因。如果没有就试试吧。