Re: updating NSView on separate thread
Re: updating NSView on separate thread
- Subject: Re: updating NSView on separate thread
- From: Kyle Sluder <email@hidden>
- Date: Mon, 1 Feb 2010 07:20:17 -0800
Ooh. So you need to perform this operation synchronously but still
pole the main thread (performing UI work is actually irrelevant; you
need to run the runloop to avoid the spinning beachball anyway).
So in your override of -writeToURL:…, spin off the background thread
like I suggested, then set up your UI, and then start running the
runloop in a special modal mode until your background thread is done.
--Kyle Sluder
On Jan 31, 2010, at 9:13 PM, Patrick Cusack
<email@hidden> wrote:
What I want to do is this: display a spinning icon in a transparent
overlay window (I can do this no problem) while my program is
saving, printing PDFs, etc. These methods as typically defined in
NSDocument do not have areas where you can inject a call to update a
progress bar. I'm thinking about - (BOOL)writeToURL:(NSURL *)
absoluteURL ofType:(NSString *)typeName error:(NSError **)outError .
I want to draw a circular-spinning-progress-ish indicator on a
transparent window so that the user understands why the UI is
temporarily locked when these methods are being called. Am I fitting
a round peg in a square whole?
Patrick
On Jan 31, 2010, at 8:09 PM, Richard Penwell wrote:
An example would be thus:
(excuse the psudo code)
In some class NSSomeController
- (void)importData
{
do
{
do soemthing...
// wants to update progress now.
[self setPercentComplete:(item / totalItems)];
[[[NSApplication] sharedApplication]
performSelectorOnMainThread:@selector(updateProgress)];
} while (true)
}
- (void)updateProgress
{
[progressControl setDoubleValue:[self getPercentComplete]];
}
On Jan 31, 2010, at 11:02 PM, Patrick Cusack wrote:
As I understand then, all drawRect methods must be made from the
main thread. If I have a process running in the main thread, like
an import thread which mike take 5 seconds, then it impossible for
me to have a secondary thread which can update an NSView
concurrently while the main thread is processing. Is this your
understanding as well?
patrick
On Jan 31, 2010, at 4:58 PM, Kyle Sluder wrote:
On Sun, Jan 31, 2010 at 4:19 PM, <email@hidden>
wrote:
I have overlaid a transparent window over my NSDocument's main
window. My intent is to draw textual notifications to it, such
as "Processing...", "20 things selected...". The idea is
partially experimental, etc. I thought it would be neat to
display a spinning icon in the a subclassed content view of the
overlay window when the program is doing something lengthy like
saving, exporting pdfs, etc. A created a new thread that would
run for the duration of an operation. This thread would call
drawrect which would draw my spinning icon. I create the new
thread before a length operation. I assume that the lengthy
operation would occur on the main thread while the drawing
operation happens on the secondary thread. Utlimately my
drawrect method doesn't get called in the secondary thread. Am I
going about this all wrong?
This is all well-trodden ground. A Google search for "NSView
secondary
thread" yields a bounty of helpful results. In summary: AppKit
isn't
thread-safe, except where explicitly documented. The general
approach
is to use -performSelectorOnMainThread: to have your secondary
thread
inform the main thread it should update the view.
--Kyle Sluder
_______________________________________________
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
_______________________________________________
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