function CompareIndirect(L: TList; Ind1: integer): integer; var Ind2: integer; TempValue: integer; begin if (Ind1 > 0) and (L.Count > Ind1) then begin L.Items[Ind1].Lock; Ind2 := L.Items[Ind1]; Assert(Ind2 <> Ind1); {I'm not even going to consider this nasty case in any more detail!} if Ind2 > Ind1 then L.Items[Ind2].Lock else begin TempValue := L.Items[Ind1].Value; L.Items[Ind1].Unlock; L.Items[Ind2].Lock; L.Items[Ind1].Lock; end; if TempValue := L.Items[Ind1].Value then Result := L.Items[Ind2].Value - L.Items[Ind1].Value else {Perhaps some retry mechanism?}; L.Items[Ind1].Unlock; L.Items[Ind2].Unlock; end else raise ENotFound; end;