call a function using thread in xcode
Asked Answered
B

3

5

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)

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.

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

Bombazine answered 3/5, 2012 at 7:42 Comment(0)
G
16

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.

You will need to properly create a thread before you can actually run something on it using 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:

[self performSelectorInBackground:@selector(func1:) withObject:nil];
Galatea answered 3/5, 2012 at 8:1 Comment(4)
thanks for reply,but some times it takes the main thread, which i don't want.....Bombazine
I'd be interested to know any information you have about performSelectorInBackground:withObject: using the main thread. The Apple documentation says "This method creates a new thread in your application, putting your application into multithreaded mode if it was not already." If you have information that shows that sometimes it uses the main thread I'd be interested to see it.Galatea
sorry the action was not taking place on main thread,since after the func1 the ':' is there,it was not running and i thought it is taking main thread.but now when i removed ':'(according to the another answer for this question) it is working fine..thanks for reply......Bombazine
To my understanding, performSelectorInBackground: creates a low priority thread. At least NSThread or detachNewThreadSelector: execute faster.Tenfold
G
3

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.

Gabionade answered 3/5, 2012 at 7:51 Comment(0)
L
0

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.

Lunchroom answered 15/3, 2013 at 5:51 Comment(1)
Whenever you are using any object of any class ,never forget to allocate the memory, as you did in your first approach.Lunchroom

© 2022 - 2024 — McMap. All rights reserved.