On Jul 20, 2008, at 4:01 PM, Torsten Curdt wrote:

AFAIU accessing UI elements from within an NSThread is a big no-no.

There are exceptions, but that's a good rule of thumb.

Here are Apple's specific guidelines: http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/chapter_950_section_2.html#/ /apple_ref/doc/uid/10000057i-CH12-123351-BBCFIIEB


So in the following naive code I would have to either wrap the calls to the progressIndicator object through a performSelectorOnMainThread or could send NSNotifications to update it on the main thread.

NSNotifications are delivered on the same thread from which they are sent/posted. So, they don't help -- in and of themselves -- for moving work from a secondary thread to the main thread.

I'm pretty sure that, at some point, you have to use performSelectorOnMainThread:... or the equivalent to pass the information about whatever progress has been made from the secondary thread to the main thread. Since some of the methods you invoke on progressIndicator take scalar values, and performSelectorOnMainThread:... requires a selector which takes an object pointer as its argument, you need to create wrapper methods in your own class to invoke on the main thread. These wrappers take their object parameter (likely an NSNumber) and "unpack" a value from it to pass along to methods of progressIndicator.

I would recommend that you do the -setMaxValue: and -startAnimation: calls from the main thread before detaching the background thread. Likewise, you should have a final method on your class which the background thread invokes using performSelectorOnMainThread:... just before it exits. That method signals that the background thread has completed its work. Among other things, that's where you would invoke -stopAnimation:.

Taken all together, you end up defining a small ad-hoc protocol between your background thread and the main thread. The background thread never actually touches the progressIndicator. Instead, it just messages self via performSelectorOnMainThread:... and the methods invoked in that way are responsible for updating progressIndicator.


I am also wondering whether using NSRunCriticalAlertPanel() in the thread is OK or not.

Good question. I don't find a specific note about that. However, the above-linked guide says this:

"You can create a modal window on a secondary thread. The Application Kit blocks the calling secondary thread while the main thread runs the modal loop."

I think that means that NSRunCriticalAlertPanel is safe to use from a secondary thread.

Cheers,
Ken
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to