On Jul 24, 2009, at 4:50 PM, Jerry Krinock wrote:

On 2009 Jul 23, at 17:44, Ken Thomases wrote:

On Jul 23, 2009, at 6:44 PM, Jerry Krinock wrote:

I'd often like to block the main thread

First question is: why? Blocking the main thread is usually bad and to be avoided. What are you actually trying to achieve? Can you give an example of when that would be desirable?

This happens any time that a user action absolutely requires some little job be done in a low-leve method before proceeding, but this job fails on rare occasions.

For example, say a user action requires that data be read in from a file known by a file alias which must be resolved. It can't continue without the resolved path, but if it's on a remote volume that was not properly dismounted, Alias Manager will beachball my app for 150 seconds before giving up.

You've presumably already moved the work to a background thread. So, why not just set a timer on the main thread to inform the user that the work is taking a long time (or whatever user interaction is appropriate)? When the background thread completes, have it perform a selector on the main thread which, among other things, can cancel the timer if it's still outstanding. Thus, if the work completes quickly, the timer never fires. This is completely asynchronous on the main thread.

If there's more to be done on the main thread after the background thread has completed, that can be initiated by the completion selector. However, it might be simplest to just have the background thread do all of the work for the task at hand, if that can be arranged.


Do consider NSConditionLock as an alternative, though. It's likely to be much simpler and more straightforward.

Yes, I've been using NSOperation for so long now that I forgot about old NSConditionLock. Although used more commonly to protect access across threads, it is in fact exactly what I need -- to block a thread until some condition.

Thanks, Ken!

You're welcome.  I'm glad it helped.

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list ([email protected])

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