Re: How to kill a thread?
Re: How to kill a thread?
On Sun, Jan 19, 2003 at 06:07:43PM +0100, Tackel wrote:
>
Hi,
>
>
I don't know how to stop a thread, any idea?
>
I have a search process running in a separate thread and want to let the
>
user to stop it. Here is an example of the code:
>
>
>
-(void)search{
>
id p=[NSAutoreleasePool new];
'new' is deprecated; use [[NSAutoreleasePool alloc] init]
while (searching) {
>
do some small chunk of search_process
}
>
>
[p release];
>
searching= FALSE;
// you need to trigger a UI update here... schedule something on
the main thread
>
}
>
>
>
- (IBAction)start:(id)sender
>
{
>
>
if (!searching) {
>
searching =TRUE;
>
[startbutton setTitle : @"stop"];
>
>
id p=[NSAutoreleasePool new];
You don't need an autorelease pool here unless you are super paranoid.
>
[NSThread detachNewThreadSelector:@selector(search) toTarget:self
>
withObject:nil];
>
>
[p release];
>
}
>
else
>
how to stop the search process???
searching = FALSE;
That's the simplest way to do it - just set a variable which the
processing checks frequently.
I would definitely suggest you read some basic Cocoa documentation
before trying multithreading - the above code scares me :) In
particular read more about memory management... lots of pointers have
been posted on this list. And if you have a button that changes
between two titles, try using the alternate title and just change the
button state, it is a lot easier (not to mention saves localization
time).
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
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.