Re: How to interrupt a long loop with command-period?
Re: How to interrupt a long loop with command-period?
- Subject: Re: How to interrupt a long loop with command-period?
- From: Matt Neuburg <email@hidden>
- Date: Sun, 30 Mar 2008 16:33:05 -0700
- Thread-topic: How to interrupt a long loop with command-period?
On Sat, 29 Mar 2008 20:45:38 -0400, Duncan Champney <email@hidden> said:
>My app can create 3D views of the fractal images it creates.
>
>It has an option to save very large versions of these 3D views to disk
>as JPEGs or TIFFs. I generate the images as tiles, then assemble them
>and save the result to disk. For very large images this can take tens
>of seconds.
>
>I have a save panel that I display as a sheet on my 3D view, and I'm
>showing a progress bar on the save panel as I build the image for
>saving. My code has to call [progress_bar display] explicitly, since I
>don't visit the event loop while I'm generating my image.
>
>I'd like to be able to have the user press command-period/escape and
>quit the save in the middle.
>
>I thought I could check for key down events with a call like this:
>
> theEvent = [[self window]
nextEventMatchingMask:(NSKeyDownMask)];
>
>But when I issue that call, everything comes to a halt until the user
>presses a key.
>
>I really want a "check the event queue for keyboard events" call that
>would let me pick command period or escape key events out of the
>applications event queue.
>
>I don't want to re-factor my code to return to the event loop if I can
>help it, both because it would be a fair amount of work, and because
>I'm using the OpenGL back buffer for my window to do my tiled
>rendering, and if the user makes changes to the window it would mess
>up my off-screen rendering. If I do have to re-factor my code to
>service the event loop I'll have to tear down the save panel sheet
>only to draw another save progress sheet in it's place, both as a
>place to show a progress bar, and to keep the user from messing up the
>save that's in progress. At least if I did that I could put a cancel
>button on it and be able to respond when the user clicks it.
I could be totally off base here, but what I do in this situation (and it
hasn't bitten me yet) is do the hard work in a thread. This leaves the main
event loop open. So, the user does whatever (press the Stop button, type Esc
or Command-period, etc.) and I hear about it immediately. I then raise a
flag (i.e. set a boolean). Meanwhile my hard-working code is periodically
checking that flag.
Now, there might be some reason why you can't do this (perhaps having to do
with your use of OpenGL), in which case just ignore that idea. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden