threads and buttons - basic question
threads and buttons - basic question
- Subject: threads and buttons - basic question
- From: Robert Palmer Jr <email@hidden>
- Date: Fri, 1 Aug 2003 17:24:00 -0400
I have, what I believe to be, a very basic question related to
messaging and threads.
I have an app with two buttons "search" and "cancel" Only one button
should ever be enabled at any given time. Press search, it becomes
disabled and cancel becomes enabled. Press cancel and it becomes
disabled and "search" is re-enabled.
When one presses search, the "search" method is called and the search
button is disabled, cancel enabled and a new thread is created that
actually does the searching. When the searching is complete, the
thread calls a method on my controller class (the same class that
detached the thread) called "searchComplete". It is in this call that
I disable the cancel button and enable the search button.
This all works fine unless my search function fails and returns very
quickly - in this case, I can get into situations where both buttons
are enabled.
It seems that probably, the [cancelButton enable:false] message in the
"searchComplete" method is getting handled before the [cancelButton
enable:true] that is sent from the "search" search method.
Is this a correct analysis - so how do I handle it?
I tried putting the button code into another method "searchCleanup" and
calling [self performSelector:@selector(searchCleanup) withObject:nil
afterDelay:0.0]; in the "searchComplete" method. In this case,
"searchCleanup" never got called.
- (IBAction)Search:(id)sender
{
[sensors removeAllObjects];
[sensorTable reloadData];
[searchButton setEnabled:false];
[cancelButton setEnabled:true];
[progressIndicator startAnimation:self];
[m_searcher setDelegate: self];
[m_searcher search]; // this method actually detaches the NSThread
}
- (IBAction)Cancel:(id)sender
{
[m_searcher cancelSearch]; // set a flag in the class to tell the
thread's loop to exit
}
- (void)searchCleanup
{
[progressIndicator stopAnimation:self]; // this code was in the
searchComplete method
[cancelButton setEnabled:false];
[searchButton setEnabled:true];
[sensorTable reloadData];
}
- (void)searchComplete
{
[self performSelector:@selector(searchCleanup) withObject:nil
afterDelay:0.0];
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.