Usina BRAdvPL Guide
Sign in
← All topics
CLASSPublished

FWGridProcess

FWGridProcess():New( cFunName, cTitle, cDescription, bProcess, [cPerg], [cGrid], [lSaveLog] )

Builds a standard interface for configuring, monitoring and running AdvPL processes, including multi-threaded work distribution.

ProcessingThreadsInterfaceLogFramework
01 · OVERVIEW

Overview

FWGridProcess organizes long-running routines into Information, SX1 Questions, Process Log and concurrency settings. A main code block controls the workflow, while an optional grid function lets CallExecute() distribute independent work units across configured threads.

02 · SYNTAX

Syntax

FWGridProcess():New( cFunName, cTitle, cDescription, bProcess, [cPerg], [cGrid], [lSaveLog] )

Parameters

cFunName
CaractereRequired

Name of the menu routine associated with the process.

cTitle
CaractereRequired

Title displayed in the interface.

cDescription
CaractereRequired

Description shown on the Information page.

bProcess
BlocoRequired

Main processing block; it receives the cancellation flag by reference.

cPerg
CaractereOptional

SX1 question group used to build the parameter page.

cGrid
CaractereOptional

Function called for each unit when grid processing is configured.

lSaveLog
LógicoOptional

Enables persistence of SaveLog() messages in SXU.

Return value

New() returns the FWGridProcess instance. IsFinished() indicates whether execution completed; CallExecute() returns the control result for the submitted work unit.

03 · PRACTICAL EXAMPLE

Main structure and Information page

#Include "TOTVS.ch"

User Function ExGridProcess()
    Local oProcess

    oProcess := FWGridProcess():New( ;
        "MATA330", "Processing", ;
        "Grid processing example", ;
        {|lCancel| U_ExProcess(oProcess, @lCancel)}, ;
        "MTA330", "U_ExUnit" ;
    )
    oProcess:SetMeters(2)
    oProcess:SetThreadGrid(5)
    oProcess:Activate()
Return
Expected result

The standard interface opens with information and execution controls.

04 · PRACTICAL EXAMPLE

SX1 question group

// MTA330 builds the Questions page.
oProcess := FWGridProcess():New( ;
    "MATA330", cTitle, cDescription, bProcess, ;
    "MTA330", "U_ExUnit" ;
)
Expected result

SX1 parameters are rendered with controls that match their data types.

05 · PRACTICAL EXAMPLE

Meters and work units

User Function ExProcess(oProcess, lCancel)
    Local nGroup
    Local nItem
    oProcess:SetMaxMeter(4, 1, "Groups")
    For nGroup := 1 To 4
        oProcess:SetMaxMeter(10, 2, "Items")
        For nItem := 1 To 10
            If !oProcess:CallExecute("Unit loaded", 1)
                lCancel := .T.
            EndIf
            oProcess:SetIncMeter(2)
            If lCancel
                Exit
            EndIf
        Next nItem
        If lCancel
            Exit
        EndIf
        oProcess:SetIncMeter(1)
    Next nGroup
Return
Expected result

Each meter follows one level of work and CallExecute() submits independent units to the grid function.

06 · PRACTICAL EXAMPLE

Process log

oProcess:SaveLog("Starting group " + cValToChar(nGroup))
ConOut(oProcess:GetLastLog())
Expected result

Messages can be viewed in the Process Log page and persisted in SXU when enabled.

BEST PRACTICES
  • Use SetMeters(), SetMaxMeter() and SetIncMeter() to represent progress levels.
  • Use SetMaxThreadGrid() to cap the user choice and SetThreadGrid() for the initial thread count.
  • CallExecute() requires a grid function configured in the constructor.
  • Use SaveLog() and GetLastLog() for execution messages.
  • SetAfterExecute(), StopExecute(), SetAbort() and SetNoParam() control the execution lifecycle and interface.
  • Activate() opens the interface; DeActivate() discards its current configuration.
COMMON PITFALLS
  • Grid functions run in separate threads; avoid uncontrolled shared state.
  • Check the by-reference cancellation flag inside long loops.
  • Choose a thread count compatible with database, licenses, memory and AppServer load.
  • Adapt RpcSetEnv(), company, branch, questions and routines before using the community sample.
  • The original sample checks nX == 5 in a loop that stops at 4; the teaching example removes this unreachable condition.

Related content

REFERENCES
  1. TOTVS. FWGridProcess. TDN Framework.
  2. Elton Teodoro. testeba.prw community sample on GitHub. Adapted and reviewed.
  3. Screenshots supplied by the AdvPL Guide editor on Aug. 5, 2026.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR