tryLock

tryLock(lockTarget:   Object;
        lockType:     Integer;
        lockDuration: Integer;
        timeout:      Integer): Boolean;

The tryLock method of the Object class attempts to acquire a lock of the specified type and duration, waiting up to the timeout period (in milliseconds) to obtain the lock on the object specified in the lockTarget parameter.

If the lock can be acquired, the method returns true. If the lock cannot be obtained, this method returns false and no lock exception is raised.

The following table lists the lock type, lock duration, and timeout system global constant values.

Global Constant Integer Value Category
Exclusive_Lock 3 Locks
Reserve_Lock 2 Locks
Share_Lock 1 Locks
Update_Lock 4 Locks
Persistent_Duration 2 LockDurations
Session_Duration 1 LockDurations
Transaction_Duration 0 LockDurations
LockTimeout_Immediate -1 LockTimeouts
LockTimeout_Infinite Max_Integer (#7FFFFFFF) LockTimeouts
LockTimeout_Process_Defined -2 LockTimeouts
LockTimeout_Server_Defined 0 (use the server-defined default) LockTimeouts

The following example shows the use of the tryLock method.

lockException(lockException: LockException): Integer;
vars
    result  : Integer;
    message : String;
begin
    message := "Cannot get lock for " & lockException.lockTarget.String
               & ". It is locked by user ";
    result := app.msgBox(message & lockException.targetLockedBy.userCode &
                         ". Retry?", "Lock Error", MsgBox_Question_Mark_Icon
                         + MsgBox_Yes_No);
    if result = MsgBox_Return_Yes then
        app.mousePointer := Busy;
        while not tryLock(lockException.lockTarget, lockException.lockType,
                          lockException.lockDuration,
                          LockTimeout_Server_Defined) do
            app.mousePointer := Idle;
            result := app.msgBox(message &
                         lockException.targetLockedBy.userCode &
                         ". Retry?", "Lock Error", MsgBox_Question_Mark_Icon
                         + MsgBox_Yes_No);
            if result = MsgBox_Return_No then
                return Ex_Abort_Action;
            endif;
            app.mousePointer := Busy;
        endwhile;
        return Ex_Resume_Next;
    else
        return Ex_Abort_Action;
    endif;
epilog
    app.mousePointer := Idle;
end;