#DEFINE kcEol CHR(13) + CHR(10) DEFINE CLASS CriticalSection AS CUSTOM OLEPUBLIC FUNCTION INIT * DCC - Declare the needed API's for the critcal section. DECLARE InitializeCriticalSection IN Kernel32 ; Integer @nHandle DECLARE INTEGER EnterCriticalSection IN Kernel32 ; INTEGER @nHandle DECLARE INTEGER TryEnterCriticalSection IN Kernel32 ; INTEGER @nHandle DECLARE LeaveCriticalSection IN Kernel32 ; Integer @nHandle lnHandle = 0 * DCC - First initialize the critical section and store the handle. llSuccess = InitializeCriticalSection( @lnHandle ) IF lnHandle > 0 =STRTOFILE( "1: " + TTOC( DATETIME() ) + " Succesfully Initalized Critical section" + kcEol, "D:\CriticalSectionLog.txt", .T. ) ELSE =STRTOFILE( "1: " + TTOC( DATETIME() ) + " Failed to Initalized Critical section" + kcEol, "D:\CriticalSectionLog.txt", .T. ) RETURN ENDIF * DCC - Then attempt to enter the critical section. ltNow = DATETIME() llSuccessfulEntry = .T. DO WHILE TryEnterCriticalSection( @lnHandle ) = 0 * DCC - Timeout after 10 seconds IF DATETIME() > ltNow + 10 llSuccessfulEntry = .F. EXIT ENDIF ENDDO * DCC - Log success or failure IF llSuccessfulEntry =STRTOFILE( "1: " + TTOC( DATETIME() ) + " Succesfully entered Critical section" + kcEol, "D:\CriticalSectionLog.txt", .T. ) ELSE =STRTOFILE( "1: " + TTOC( DATETIME() ) + " Failed to enter Critical section" + kcEol, "D:\CriticalSectionLog.txt", .T. ) RETURN ENDIF * DCC - Leave criticla section. =LeaveCriticalSection( @lnHandle ) =STRTOFILE( "1: " + TTOC( DATETIME() ) + " Left Critical section" + kcEol, "D:\CriticalSectionLog.txt", .T. ) ENDFUNC ENDDEFINE