Re: Question on thread communication
Re: Question on thread communication
- Subject: Re: Question on thread communication
- From: Chris Kane <email@hidden>
- Date: Mon, 17 Apr 2006 10:51:31 -0700
On Mar 20, 2006, at 11:39 AM, Karim Morsy wrote:
I've done this as follows:
on the main thread I detach a new thread that performs a task. upon
completion of the thread the main thread should perform subsequent
steps. it shouldn't do so unless the detached thread has really
finished execution. for the detached thread to signal to the main
thread that it's done, I do this:
NSNotificationCenter *nc= [NSNotificationCenter defaultCenter];
NSNotification *n= [NSNotification notificationWithName:
KMFileInitCompletedNotification object:self];
[nc performSelectorOnMainThread:@selector(postNotification:)
withObject:n waitUntilDone:YES];
[pool release];
the completion steps are then performed by the main thread in the
appropriate handler method.
so the questions I have are:
- is there anything problematic about this approach ?
- is there an easier/better way to do this ?
To add another comment for the archives ...
It works, but there's no reason to go indirectly through the
notification center, if you have only one thing that needs to react to
the notification (though it's not clear you do, you say "handler
method" -- singular). You could just perform a method on that object
directly, passing whatever argument you like.
That avoids the indirection of the notification object (and packaging
in the userInfo dictionary whatever "results" you might want to send
from the background thread to the main thread), and the hassle of
adding the recipient object to the notification center and removing it
later.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden