User Tools

Site Tools


cascaded_20on_20close_20handling

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cascaded_20on_20close_20handling [2018/03/31 13:19] – external edit 127.0.0.1cascaded_20on_20close_20handling [2024/01/05 00:22] (current) – external edit 127.0.0.1
Line 1: Line 1:
 =====Cascaded ON CLOSE handling===== =====Cascaded ON CLOSE handling=====
  
-//by Richard Russell, September 2010//\\ \\  The article [[/Cascaded%20error%20handling|Cascaded error handling]] explains how a procedure or function can take some 'local' remedial action in the event of an error (for example to cleanup after a **Windows API** operation) and then pass responsibility for dealing with the error to the code from which it was called. In principle there may be several cascaded procedures or functions, each of which needs to handle the error before passing it 'down the chain'.\\ \\  A very similar situation can occur when the user closes the window. The procedure or function which is being executed when the **ON CLOSE** event occurs may need to perform some 'local' cleanup operations before the 'global' ON CLOSE handler finally quits the program. Again, in principle there may be several cascaded procedures or functions which each need to handle the ON CLOSE event.\\ \\  The most elegant way to achieve this is to combine the cascaded ON ERROR and ON CLOSE handling, by allocating an error number (**111** in this example) to the ON CLOSE event. The short program below illustrates this technique; the user is prompted either to press **Escape** (which results in an error) or to click **Close** (which triggers the ON CLOSE event). The code waits in an 'infinite loop' within **PROC2** until one or other of these happens.\\ \\  After the event is handled in **PROC2** it is passed down to **FN1** and then to **PROC0**; finally the catch-all ON ERROR or ON CLOSE statement in the main program is activated. For the purposes of illustration, the handling of the ON ERROR and ON CLOSE events at each stage consists simply of printing a message to the screen; in a practical example this would be replaced by the necessary cleanup code. \\ \\ +//by Richard Russell, September 2010//\\ \\  The article [[/Cascaded%20error%20handling|Cascaded error handling]] explains how a procedure or function can take some 'local' remedial action in the event of an error (for example to cleanup after a **Windows API** operation) and then pass responsibility for dealing with the error to the code from which it was called. In principle there may be several cascaded procedures or functions, each of which needs to handle the error before passing it 'down the chain'.\\ \\  A very similar situation can occur when the user closes the window. The procedure or function which is being executed when the **ON CLOSE** event occurs may need to perform some 'local' cleanup operations before the 'global' ON CLOSE handler finally quits the program. Again, in principle there may be several cascaded procedures or functions which each need to handle the ON CLOSE event.\\ \\  The most elegant way to achieve this is to combine the cascaded ON ERROR and ON CLOSE handling, by allocating an error number (**111** in this example) to the ON CLOSE event. The short program below illustrates this technique; the user is prompted either to press **Escape** (which results in an error) or to click **Close** (which triggers the ON CLOSE event). The code waits in an 'infinite loop' within **PROC2** until one or other of these happens.\\ \\  After the event is handled in **PROC2** it is passed down to **FN1** and then to **PROC0**; finally the catch-all ON ERROR or ON CLOSE statement in the main program is activated. For the purposes of illustration, the handling of the ON ERROR and ON CLOSE events at each stage consists simply of printing a message to the screen; in a practical example this would be replaced by the necessary cleanup code. 
 + 
 +<code bb4w>
         PRINT "Click Close  to test cascaded ON CLOSE handling"         PRINT "Click Close  to test cascaded ON CLOSE handling"
         PRINT "Press Escape to test cascaded ON ERROR handling"         PRINT "Press Escape to test cascaded ON ERROR handling"
Line 39: Line 41:
  
         DEF PROCclose         DEF PROCclose
-        IF !392 PROC(^@%+392) ELSE QUIT+        LOCAL c%% 
 +        c%% = PAGE - !340 + !392 
 +        IF !392 PROC(^c%%) ELSE QUIT
         ENDPROC         ENDPROC
 +</code>
 +
 Note that in a real-world example the final ON CLOSE handler would probably **QUIT** the program. That isn't done here because the displayed output would disappear!\\ \\  There is a theoretical possibility that an ON CLOSE event could occur //between// the **ON CLOSE** and **ON ERROR** statements in PROC0. In that very unlikely circumstance, the close would be handled as an error. Apart from the user receiving an unexpected message, it should be harmless. Note that in a real-world example the final ON CLOSE handler would probably **QUIT** the program. That isn't done here because the displayed output would disappear!\\ \\  There is a theoretical possibility that an ON CLOSE event could occur //between// the **ON CLOSE** and **ON ERROR** statements in PROC0. In that very unlikely circumstance, the close would be handled as an error. Apart from the user receiving an unexpected message, it should be harmless.
cascaded_20on_20close_20handling.1522502347.txt.gz · Last modified: 2024/01/05 00:18 (external edit)