Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Unique key generator
Message
De
04/01/2000 20:40:35
 
 
À
04/01/2000 19:50:29
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00312803
Message ID:
00312942
Vues:
33
The following code ran without throwing an error for the candidate index. Note that I changed from an FLOCK() to an RLOCK(). With FLOCK() it seemed that one process would get the exclusive access to the the table for longer periods. Perhaps the locate gives the other processes a better chance to aquire a lock. In processes that aren't so tightly looped, that probably is not a problem.
#define KEY_LEN	10

set exclusive off
set reprocess to 300

if !file("system.dbf")
  create table system free (cTable c(254), cKeyValue i)
  insert into system (cTest) values (0)
  use
endif

if !file("results.dbf")
  create table results free (cTest c(KEY_LEN))
  index on ctest tag ctest candidate
  use
endif

set escape on
do while .t.
  insert into results (cTest) values (NextNumber("cTest", KEY_LEN))
enddo

*-------------------------------------------------
function NextNumber
  * get the next incremental number for the passed field, padded with
  * leading zeros

  ****    PARAMETER cField,nLen
  lparameters cField, nLen
  local cOldAlias, lUsed, lDone, cReturnNum
  
  cOldAlias = alias()
  lUsed = used( "System" )
  if ! lUsed
    use system in 0
  endif
  select system
  lDone = .f.
  do while ! lDone
  	locate
    if rlock()
      ****            nReturnNum = &cField
      ****            REPLACE &cField WITH &cField + 1
      replace (cField) with eval(cField) + 1
      cReturnNum = padl( eval(cField), nLen, '0' )
      unlock
      lDone = .t.
    else
      inkey(.5)
    endif
  enddo
  if ! empty( cOldAlias )
    select (cOldAlias)
  endif
  if ! lUsed
    use in system
  endif
  return cReturnNum
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform