Re: Challenge: Block Main Thread while Work is done, with Timeout
Re: Challenge: Block Main Thread while Work is done, with Timeout
- Subject: Re: Challenge: Block Main Thread while Work is done, with Timeout
- From: Graham Cox <email@hidden>
- Date: Sat, 25 Jul 2009 11:27:22 +1000
On 25/07/2009, at 7:50 AM, Jerry Krinock wrote:
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.
The right approach here isn't to block the main thread but harness it
to your (and the user's) advantage.
Display a modal progress window which tells the user's what's going
on, show an indeterminate progress bar and include a "Stop" button.
The worker code can wait on the alias being resolved and the main
thread can keep the user informed. By using a modal progress window
you've effectively "blocked" the user from doing anything but if they
are not prepared to wait they have a way to abort the operation (which
you have to implement of course). Just blocking without telling the
user what's going on is worse than just beachballing - the beachball
is after all a feedback mechanism that tells the user *something*.
It's straightforward to write a generic progress controller class that
can automatically wait a few seconds before displaying itself, so if
the operation does complete quickly, the progress dialog isn't shown
but there's no need on the part of the worker code to special-case for
that - just start progress at the beginning and end progress at the
end. The dialog will run the main thread in a modal loop.
--Graham
_______________________________________________
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