• 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: Pausing and terminating a thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pausing and terminating a thread


  • Subject: Re: Pausing and terminating a thread
  • From: email@hidden
  • Date: Tue, 30 Aug 2005 08:26:11 -0500

I think to end the thread you still need some sort of "poll". I've written a threaded object to download records in the background; it "schedules" units of work through the run loop using performSelector: and has a timer. The timer has a very short duration -- it counts expirations before killing things off -- and takes care of the thread exit by invalidating itself when there's no work and the timer expires.

The timer method also checks an ivar "stop" which can be set by the main thread; if true, it schedules calling a connectionWasCancelled delegate method on the main thread before ending the worker thread. Since I need a timer in the background thread anyway, it ends up "polling."

Which brings me to the real reason I'm writing this. Having looked at your web page (nice, BTW), your thread should likely run in some object which calls delegate methods (which you'll have to define) on your document or app controller. Why? Because they should be called with performSelectorOnMainThread: in order to allow your main object (s) to update the GUI safely. Not doing this may be a cause for the weird behavior you're seeing. I would include in the work that should only be done on the main thread not only explicit GUI updates like updating a progress bar, but indirect updates through bindings.

You might have a couple of delegates, didGenerateRange and didFinish, so that your main object can release the threaded object once its done. I will post some example code (ugly but functional) on my web site (which, I have to say, you inspired -- I even ended up using the same them in RapidWeaver ;-).

Matt Holiday -- http://homepage.mac.com/matthol2/cocoa/page1/page1.html

Message: 13
Date: Mon, 29 Aug 2005 23:23:08 -0700
From: Steve Weller <email@hidden>
Subject: Re: Pausing and terminating a thread

So I can use a lock to implement the pause -- neat. Just acquire the
lock in my main thread and that will stop the worker thread from
continuing until I release it.

However this still requires that my worker thread poll. And doesn't
take care of quitting the worker thread. Any better way?

On Aug 29, 2005, at 11:14 PM, j o a r wrote:

On 30 aug 2005, at 08.08, Steve Weller wrote:

I have implemented pausing and terminating in a worker thread
(NSThread) by polling a state variable updated by the main thread
and acting accordingly. This works fine, but is there a way to
make the worker thread pause/continue or exit from my main thread
so I don't have to do the polling?

NSConditionLock?

_______________________________________________ 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
  • Prev by Date: Re: NSKeyedArchiver - alternatives for speed improvements?
  • Next by Date: Re: Problem with converting an NSImage to grayscale
  • Previous by thread: Re: Pausing and terminating a thread
  • Next by thread: Re: Pausing and terminating a thread
  • Index(es):
    • Date
    • Thread