home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / gt_locki.prg < prev    next >
Text File  |  1993-10-14  |  3KB  |  122 lines

  1. /*
  2.     File......: GT_Locking.prg
  3.     Author....: Martin Bryant
  4.     BBS.......: The Dark Knight Returns
  5.     Net/Node..: 050/069
  6.     User Name.: Martin Bryant
  7.     Date......: 04/02/93
  8.     Revision..: 1.0
  9.  
  10.     This is an original work by Martin Bryant and is placed
  11.     in the public domain.
  12.  
  13.     Modification history:
  14.     ---------------------
  15.  
  16.     Rev 1.0 04/02/93
  17.     PD Revision.
  18. */
  19.  
  20. /*  $DOC$
  21.  *  $FUNCNAME$
  22.  *      GT_LOCKING()
  23.  *  $CATEGORY$
  24.  *      General
  25.  *  $ONELINER$
  26.  *      Function to lock a datafile/record and return the success
  27.  *  $SYNTAX$
  28.  *       GT_Locking(<lFile>,[<nSeconds>],[<nPause>]) -> lSuccess
  29.  *  $ARGUMENTS$
  30.  *      <lFile> is a logical switch, .T. = Lock the file
  31.  *      and .F. is just the record.
  32.  *
  33.  *      <nSeconds> is the number of seconds to wait before
  34.  *      giving up.
  35.  *
  36.  *      <nPause> is thetime interval to pause for before a
  37.  *      retry.
  38.  *  $RETURNS$
  39.  *      GT_Locking() returns logical success.
  40.  *  $DESCRIPTION$
  41.  *      Function to lock a datafile/record and return the
  42.  *      success. If at first it fails it will wait the
  43.  *      specified number of seconds before aborting.
  44.  *  $EXAMPLES$
  45.  *      // Reset a counter
  46.  *      IF GT_Locking(.F.,60,0.5)
  47.  *          Field->Count := 0
  48.  *      ELSE
  49.  *          ? 'Error'
  50.  *      ENDIF
  51.  *  $SEEALSO$
  52.  *
  53.  *  $INCLUDE$
  54.  *
  55.  *  $END$
  56.  */
  57.  
  58. #include "GT_LIB.ch"
  59.  
  60. #define HEIGHT  06
  61. #define WIDTH   16
  62.  
  63. FUNCTION GT_Locking(lFile,nSeconds,nPause)
  64.  
  65. LOCAL cScreen := ''
  66. LOCAL lMessage := .F.
  67. LOCAL lSuccess := .F.
  68. LOCAL nBottom := INT((MAXROW() + HEIGHT)/2)
  69. LOCAL nKey := 0
  70. LOCAL nLeft := INT((MAXCOL() - WIDTH)/2)
  71. LOCAL nRight := nLeft - WIDTH
  72. LOCAL nTop := nBottom - HEIGHT
  73.  
  74. Default lFile to .F.
  75. Default nSeconds to 120
  76. Default nPause to 0.5
  77.  
  78. DO WHILE nSeconds > 0 .AND. (.NOT. lSuccess) .AND. ;
  79.     (nKey != K_ESC)
  80.  
  81.     // Attempt lock
  82.     lSuccess := IF(lFile,FLOCK(),RLOCK())
  83.  
  84.     IF .NOT. lSuccess
  85.  
  86.         // Message ?
  87.         IF .NOT. lMessage
  88.  
  89.             // Save
  90.             cScreen := SAVESCREEN(nTop,nLeft,nBottom,nRight)
  91.  
  92.             // Display
  93.             GT_Window(nTop,nLeft,nBottom,nRight,BOX_SS, ;
  94.                 NIL,'Locking ....',.T.)
  95.  
  96.             // Position for message
  97.             @ nTop+02, nLeft+02 SAY 'Timeout:'
  98.             @ nTop+04, nLeft+02 SAY PADC('Esc∙Exit',WIDTH-03)
  99.  
  100.             lMessage := .T.
  101.  
  102.         ENDIF
  103.  
  104.         @ nTop+02, nLeft+09 SAY PADR(INT(nSeconds),3)
  105.  
  106.         // Wait
  107.         nKey := INKEY(nPause)
  108.         nSeconds -= nPause
  109.  
  110.     ENDIF
  111.  
  112. ENDDO
  113.  
  114. IF lMessage
  115.     RESTSCREEN(nTop,nLeft,nBottom,nRight,cScreen)
  116. ENDIF
  117.  
  118. /*
  119.     End of GT_Locking()
  120. */
  121. RETURN(lSuccess)
  122.