Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

Preventing and diagnosing AdvPL loops

Makes progress, exit conditions and loop limits explicit.

Preventing and diagnosing AdvPL loopsAdvPL
01 · OVERVIEW

Overview

Identify what changes the continuation condition before optimizing a loop. For query cursors, qualify Eof and DbSkip with the query alias even when the body positions another table. Loop can skip advancement at the end of the body; organize each path to advance or exit. This memory-only example has a work limit and a business condition that stops earlier. Diagnose with counters and technical identifiers without personal data; compare progress and check unlimited retries, locks and reentrant validation. (Sources: refs. 1–2.)

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed597()
    Local nStep := 0
    Local nLimit := 10
    // Every iteration advances; the cap bounds the work.
    While nStep < nLimit
        nStep++
        If nStep == 3
            Exit
        EndIf
    EndDo
    ConOut(nStep)
Return Nil
Expected result

Prints 3 with explicit progress and exit. A cap does not replace error handling or cancellation. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • A counter cannot interrupt a blocking inner call. Measure the slow section and check each operation alias.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. Desenvolvendo queries no Protheus. Seções de varredura e query de recno. Accessed on 2026-10-27.
  2. Atilio. dan-atilio/AdvPL. Maratona de Exemplos/Exemplo_421_Funcao_RetSQLName.prw. Arquivo completo consultado; adaptação editorial. Accessed on 2026-10-27.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha