NSThread, drawing and waiting
NSThread, drawing and waiting
- Subject: NSThread, drawing and waiting
- From: Damien Sorresso <email@hidden>
- Date: Tue, 26 Jul 2005 16:33:25 -0500
I'm having one hell of a time with multithreading my app. I to keep a
progress bar in the interface up-to-date with the progress of a search.
The progress bar is a part of an `NSView', which is the delegate for
the class which actually performs the search.
Here's what the calling stack looks like:
[RNRequirement searchForItem]
[RNRequirementManager beginSearches]
[ScanViewController scan]
[MainWindowController scan] (IBAction)
The `RNRequirementManager' has an array of `RNRequirement' items that
it goes through and calls the ``searchForItem" methods for. Here's what
that code looks like.
for( i = 0; i < numResults; i++ ) {
int condition = [[requirementsArr objectAtIndex:i] lockCondition];
[conditionLock lock];
[conditionLock unlockWithCondition:condition];
[NSThread detachNewThreadSelector:@selector(searchForItem)
toTarget:[requirementsArr objectAtIndex:i]
withObject:nil];
[conditionLock lockWhenCondition:0];
[conditionLock unlock];
// Tell delegate what to do
// ...
}
``lockCondition", in the `RNRequirement' object, is set to `1' at the
beginning of the search and `0' when the search completes. So when it
hits `0', I want to tell the delegate that the search for that item has
completed. But it never gets past the lock.
Just letting the `for' loop go through without spinning off into
another thread completely hangs the UI, and only after everything is
done does the progress bar draw.
I also tried using a busy `while' loop instead of thread locks. I'd
initialize `condition' to ``[[requirementsArr objectAtIndex:i]
retainCount] + 1" and then put a busy `while' loop right after the
thread detachment to the effect of
while( condition == [[requirementsArr objectAtIndex:i] retainCount] ) {
condition = condition;
}
(Since the parameter for `toTarget' is sent a ``retain" upon being spun
off into the new thread, the thread will exit and leave the object with
a `retainCount' of 1 less. So ``condition" is set for what the retain
count WILL be.)
This sort of worked, even though it's an awful way of doing things.
Basically what happened is that I logged the calls to the delegate, and
they all took place at the proper times, but the `NSView' didn't update
anything.
Any help is greatly appreciated.
--
Damien Sorresso
Macintosh Developer
Computer Infrastructure Support Services
Illinois State University
email@hidden
309.438.5777
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden