Re: Really Big Windows and Interrupting computations?
Re: Really Big Windows and Interrupting computations?
- Subject: Re: Really Big Windows and Interrupting computations?
- From: Paul Collins <email@hidden>
- Date: Tue, 22 Jun 2004 17:23:04 -0700
Threads are great, but I don't think you need them for this...
If you want to do a long computation that can be interrupted, one way
is to use an NSTimer. By setting an NSTimer to fire after a short time
and then returning from the current method, you are assured that the
run loop will go through any inputs (for example, your Quit message).
You'll have to factor your computation into a method that is called
repeatedly (by the NSTimer) and does one short chunk of the work at a
time. Save the state after each chunk in instance variables that the
method checks each time it starts up.
In your quit method, invalidate the timer (also invalidate it in
-dealloc so you don't get the timer calling your class after it's
gone!).
By the way, it's tempting to use the "repeats" version of NSTimer, but
I've found it's rather troublesome in terms of getting your program
logic right. Instead, just create a brand new NSTimer at the end of
your special repeating method (and put it in an instance variable so
the quit method can find it and invalidate it).
Also, NSRunLoop performSelector:target:argument:order:modes: can be
handy in a similar way.
--Paul Collins
P.S. Looking at your web site, I fear I've put in a lot of things you
already know, but too late now--anybody who's figured out the Power
Manager and Security Manager probably doesn't need advice from me :-)
On Jun 22, 2004, at 1:16 PM, Jerry LeVan wrote:
I have a tight loop that looks like
put up a progress window
quit=NO
for i = 1 to some (possibly) big number
a(i) = some (upto )several second computation ( scaling big images
)
force update of progress window with "displayIfNeeded"
if (quit) break
end
** Now in Public beta!
http://www.opendoor.com/envision/ **
** Envision: a new way to experience the web **
** Gracion Software
http://www.gracion.com/ **
** Tel: +1 (541) 488-3488 iChat/AV/AIM: gracionsoft **
** DigiTunnel 1.3 PPTP/VPN client for OS X **
_______________________________________________
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.