home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / rexx / greply.cmd < prev    next >
OS/2 REXX Batch file  |  1999-05-11  |  2KB  |  45 lines

  1. /******************************************************************************/
  2. /*  greply                   Object REXX Samples                              */
  3. /*                                                                            */
  4. /*  Using the GUARD instruction to control methods                            */
  5. /*                                                                            */
  6. /*  This program demonstrates the difference between Guarded and UnGuarded    */
  7. /*  methods.  In the first example, the method is guarded, so it does not     */
  8. /*  begin execution until the final result is tallied.  In the second         */
  9. /*  example, the method is unguarded so it can begin execution while method   */
  10. /*  sum_up is still looping.  In fact, unguarded_get often runs immediately   */
  11. /*  after the Reply, so we use a guard instruction to ensure sum_up runs for  */
  12. /*  a bit before unguarded_get returns with the intermediate sum.             */
  13. /******************************************************************************/
  14.  
  15. /* guarded_get will wait until sum_up has completed                           */
  16. Say 'Using GUARDED method to wait until sum is complete:'
  17. Say 'Result should be 12502500:' .counter~new~~sum_up(5000)~guarded_get
  18. Say ''
  19.  
  20. /* unguarded_get will begin execution before sum_up has completed             */
  21. Say 'Using UNGUARDED method to obtain an intermediate result:'
  22. Say 'Result should be less than 12502500:' .counter~new~~sum_up(5000)~unguarded_get
  23. Exit
  24.  
  25.  
  26. ::CLASS counter
  27. ::Method sum_up
  28.   Expose o_var
  29.   o_var = 0                                 /* Initialize our counter         */
  30.   Reply o_var                               /* Early reply so others may run  */
  31.   Do i = 1 to arg(1)                        /* Loop here for a bit            */
  32.     o_var = o_var + i
  33.                                             /* Let them know we're still here */
  34.     If i//1000 = 0 Then Say 'Inside method sum_up:  loop iteration' i
  35.   End
  36.  
  37. ::Method guarded_get                        /* GUARD is default               */
  38.   Expose o_var
  39.   Return o_var
  40.  
  41. ::Method unguarded_get unguarded
  42.   Expose o_var
  43.   Guard off when o_var > 9999               /* Wait until count progresses a bit */
  44.   Return o_var                              /* Return intermediate result     */
  45.