• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to kill a thread?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to kill a thread?


  • Subject: Re: How to kill a thread?
  • From: Nicholas Riley <email@hidden>
  • Date: Sun, 19 Jan 2003 11:34:56 -0600
  • Mail-followup-to: Tackel <email@hidden>, email@hidden

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.

References: 
 >How to kill a thread? (From: Tackel <email@hidden>)

  • Prev by Date: Re: Memory management questions using UUID example
  • Next by Date: Re: Memory management questions using UUID example
  • Previous by thread: How to kill a thread?
  • Next by thread: Re: How to kill a thread?
  • Index(es):
    • Date
    • Thread