Stopping a function before it is finished.
[1/2] from: reboler::programmer::net at: 29-Oct-2002 8:50
I have a script which shows the contents of files, and performs some _lengthy_ processing of their contents. I have simulated the essentials in a script given below. Suppose that during processing, I notice that I have entered the wrong password, and now I want to stop processing the file immediately, instead of waiting til it is done. (for instance, by clicking on a new file in the text-list) How would you REBOL gurus go about this? Seems to me I should add a 'wait somewhere, but where? The 'process-file function? Or the button '/action? (this is my preference, since I have many processing functions!) Elsewhere ??? I've tried a few things but nothing seems to work. ;*** begin script view layout [ ;some setup do [ files: read %./ process-file: func [ file-contents ][ ;some lengthy processing simulated here file-contents: enbase/base compress form view-area/text 64 loop 500 [ for n 1 length? password/text 1 [ new-char: (xor to-char pick password/text :n to-char pick file-contents :n) poke file-contents n new-char ] file-contents: compress form enbase/base file-contents 64 ] ] ] text-list data files [ view-area/text: read to-file value show view-area ] button "Process File" [ view-area/text: process-file view-area/text show view-area ] return text "password" password: field "password" view-area: area 320x240 ];end layout ;*** end script
[2/2] from: greggirwin:mindspring at: 29-Oct-2002 9:53
Hi Alan, << Suppose that during processing, I notice that I have entered the wrong password, and now I want to stop processing the file immediately, instead of waiting til it is done. >> What I've done quite a bit in the past is designed the processing code to check for an 'abort flag periodically during processing. The flag is generally set by some user action or change in the system. That's not always a reliable system, depending on how the underlying event processing is done, and is also subject to changing behaviors due to changes in the underlying event system. For that reason, my preferred solution is to use a state machine that is fed events, one of which may be an 'abort event naturally. The cleanest, simplest, most robust solution is sometimes found after trying a few bad ones. :) --Gregg