• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Challenge: Block Main Thread while Work is done, with Timeout
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Challenge: Block Main Thread while Work is done, with Timeout


  • Subject: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • From: Ken Thomases <email@hidden>
  • Date: Fri, 24 Jul 2009 22:39:52 -0500

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@hidden)

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:
This email sent to email@hidden


References: 
 >Challenge: Block Main Thread while Work is done, with Timeout (From: Jerry Krinock <email@hidden>)
 >Re: Challenge: Block Main Thread while Work is done, with Timeout (From: Ken Thomases <email@hidden>)
 >Re: Challenge: Block Main Thread while Work is done, with Timeout (From: Jerry Krinock <email@hidden>)

  • Prev by Date: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • Next by Date: Unit testing question
  • Previous by thread: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • Next by thread: Re: Challenge: Block Main Thread while Work is done, with Timeout
  • Index(es):
    • Date
    • Thread