Re: Sending image from separate thread back to AppController
Re: Sending image from separate thread back to AppController
- Subject: Re: Sending image from separate thread back to AppController
- From: Shaun Wexler <email@hidden>
- Date: Wed, 21 Jul 2004 00:18:40 -0700
On Jul 20, 2004, at 1:44 PM, Andrew Duncan wrote:
My AppController creates a new thread (call it Thread2), which runs
and, in its own good time, creates an NSImage. Now I want to tell the
main (UI) thread to display this image. Of course, Thread2 knows
nothing about the outlets to the NSImageView, so it has to ask the
AppController to deal with this.
What's the Best Practice for this one? First impulse is to give the
new thread a pointer back to the AppController. Then Thread2 can call
a method on the AppController, sending the image (pointer) and having
the AppController call setNeedsDisplay on the imageView.
But maybe there's some cool notification scheme or value observer or
something in Panther that I don't know about. I'm sure this is an FAQ
and would appreciate hearing the frequent answer.
This also works, if you just want to ensure that your AppController
sets the new image while running on the main thread:
- (void)setNewImage:(NSImage *)image
{
if (pthread_main_np()) {
[myImageView setImage:image];
} else {
[self performSelectorOnMainThread:_cmd withObject:image
waitUntilDone:NO];
}
}
This method is perfectly safe to call from other threads...
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.