On Sat, Mar 14, 2009 at 1:29 PM, Paul Sanders <p.sand...@dsl.pipex.com> wrote: >> So: what should be done? > > 1. Do the calculations in a sub-thread. > > 2. Test a 'please stop' variable each time round the loop in that thread. > > 3. Set this variable in the main thread when the user clicks the Stop > button.
Better yet, test NSThread's isCanceled in your loop, and use the cancel method. Obviously, only works if you don't support Tiger. > 4. To show progress, call performSelectorOnMainThread from the sub-thread. > Do this every, say, 1000 loop iterations for efficiency. That method (so to > speak) is safe. I'm not sure if other techniques are safe or not, but my > guess is that they aren't. Rather than doing it every 1000 iterations, make it be time-based. Check the time since the last notification, and only message the main thread if the current time is more than, say, 1/30th of a second later. There's no point in updating the GUI more than 30Hz or so, and this makes it so that it doesn't matter how long each individual iteration takes to run. Depending on how long you expect the full operation to take, you might update substantially less often than this. I'm not sure what you're referring to when you talk about "other techniques", but in general it is forbidden to access the GUI of a Cocoa app from secondary threads, so you pretty much have to use performSelectorOnMainThread: or some equivalent to update your GUI. Mike _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com