Re: Threads and widgets
Re: Threads and widgets
- Subject: Re: Threads and widgets
- From: "Mike R. Manzano" <email@hidden>
- Date: Sun, 6 Mar 2005 18:25:31 -0800
I figured this one out I think. If I define these methods;
- (void) postCanonicalNameNotification
{
NSLog( @"%@/postCanonicalNameNotification", [ NSThread currentThread ]
) ;
[ [ NSNotificationCenter defaultCenter ]
postNotificationName:kCanonicalNamesModifiedNotification
object:self ] ;
} // postCanonicalNameNotification
- (void) postCanonicalNameNotificationLater
{
[ self performSelector:@selector(postCanonicalNameNotification)
withObject:nil afterDelay:1.0 ] ;
} // postCanonicalNameNotificationLater
and then call postCanonicalNameNotificationLater like this from my
other thread:
[ self performSelectorOnMainThread:
@selector(postCanonicalNameNotificationLater)
withObject:nil waitUntilDone:NO ] ;
everything seems to work correctly. When the NSPopUpButton is open (I
clicked on it), it actually seems to pause the main thread until I make
my selection. However, if I don't do the performSelector after a delay,
even if it's being performed on the main thread, the main thread does
not stop while I have the NSPopUpButton open, eventually causing a
crash. This is puzzling to me. Although I seemed to have fixed my
problem, I'd love to know if this is appropriate behavior for
NSPopUpButton to not pause the main thread unless it is done after a
delay.
In other words, to beat a dead horse, from my other thread, why can't I
just:
[ self performSelectorOnMainThread:
@selector(postCanonicalNameNotification)
withObject:nil waitUntilDone:NO ] ;
and expect it to work?
I hope I'm not being too confusing.
Mike
On Mar 5, 2005, at 12:27 AM, Mike R. Manzano wrote:
Okay, here's one I can't figure out. I have this code being called
from a thread other than the main thread:
NSLog( @"%@/Scheduling postCanonicalNameNotification" , [ NSThread
currentThread ] ) ;
[ self performSelectorOnMainThread:
@selector(postCanonicalNameNotification)
withObject:nil waitUntilDone:NO ] ;
Here is the selector being performed:
- (void) postCanonicalNameNotification
{
NSLog( @"%@/postCanonicalNameNotification", [ NSThread currentThread
] ) ;
[ [ NSNotificationCenter defaultCenter ]
postNotificationName:kCanonicalNamesModifiedNotification
object:self ] ;
} // postCanonicalNameNotification
As you can see, this posts a notification on the default center. Here
is the one and only observer:
- (void) handleCanonicalNamesModifiedNotification: (NSNotification*)
notification
{
NSLog( @"cMainWindowController is handling
kCanonicalNamesModifiedNotification" ) ;
// Update the participants list
cFSModel* fsm = [ cFSModel instance ] ;
NSArray* participants = [ fsm allParticipantsAsCanonicalNames ] ;
// Sort them
participants = [ participants
sortedArrayUsingSelector:@selector(compare:) ] ;
// Remove all participants
[ _participants removeAllItems ] ;
// Add them back in
[ _participants addItemWithTitle:@"Show Everyone" ] ;
[ _participants addItemsWithTitles:participants ] ;
[ _participants setEnabled:YES ] ;
NSLog( @"cMainWindowController done handling
kCanonicalNamesModifiedNotification" ) ;
} // handleCanonicalNamesModifiedNotification:
According to the logs, the (non-main) thread is scheduling
postCanonicalNameNotification to be executed on the main thread, and
it is indeed being executed on the main thread. From what I understand
of notifications, handleCanonicalNamesModifiedNotification: should
subsequently execute on the main thread, and indeed it does.
Now, this seems to work just fine, and it works flawlessly every time.
However, if I click on the _participants widget (an NSPopupButton),
and play with it while it is open, the program eventually crashes. I'm
thinking it loses its brain because I am changing its menu items while
I'm playing with the widget at the same time, but I'm not sure if
that's the problem. Since the menu item updates are occurring in the
main thread, there shouldn't really be any race conditions, right?
Some other possibly important info: postCanonicalNameNotification: is
called fairly often (at least once a second), and
handleCanonicalNamesModifiedNotification: executes even when the popup
button is showing its menu (it doesn't block execution of the main
thread).
Can anyone help my trace down this crash?
Thanks,
Mike
_______________________________________________
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