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

Last revision Both sides next revision
cascaded_20on_20close_20handling [2018/03/31 13:19]
127.0.0.1 external edit
cascaded_20on_20close_20handling [2018/04/14 20:39]
richardrussell Added syntax highlighting
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 41: Line 43:
         IF !392 PROC(^@%+392) ELSE QUIT         IF !392 PROC(^@%+392) 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.txt · Last modified: 2018/08/01 14:56 by richardrussell