Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Unique key generator
Message
From
04/01/2000 20:40:35
 
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00312803
Message ID:
00312942
Views:
34
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
Previous
Reply
Map
View

Click here to load this message in the networking platform