IR
irwinrodriguez.dev
Back to docs

Quick Start

Your first background task in under 5 minutes.

Prerequisites

  • FoxCore installed and licensed
  • VFP 9 SP2 or higher
  • Task class must be in a compiled PRG or class inside an APP/DLL

1. Create a FoxCoreClass instance

Instantiate the main object. All task management is done through this object.

LOCAL loCore
loCore = CREATEOBJECT("FoxCore.FoxCoreClass")
? loCore.Version   && prints the installed version

2. Define the task procedure

The task procedure receives two parameters: loParams (input data) and loTask (the Task object for reporting progress and result). It must be in a compiled PRG, not in the command window.

* MyTasks.prg  (must be SET PROCEDURE TO or compiled into APP)
PROCEDURE MyBackgroundTask(loParams, loTask)
    LOCAL i
    FOR i = 1 TO loParams.iterations
        * Simulate work
        INKEY(0.1)
        loTask.Progress = INT((i / loParams.iterations) * 100)
    NEXT
    loTask.Result = "Done! Processed " + TRANSFORM(loParams.iterations) + " items."
ENDPROC

3. Run the task

Call Run() with the procedure name and the parameters object. The call returns immediately with a Task object.

LOCAL loCore, loParams, loTask
loCore   = CREATEOBJECT("FoxCore.FoxCoreClass")
loParams = CREATEOBJECT("Empty")
ADDPROPERTY(loParams, "iterations", 50)
loTask = loCore.Run("MyBackgroundTask", loParams)
? loTask.Id      && GUID of the new task
? loTask.Status  && "Pending" or "Running"

4. Check status (non-blocking)

Use the Task's Status property to know the current state. The most common pattern is to read it in a Timer event to avoid blocking the UI.

* Poll in a Timer event (interval 500 ms recommended)
IF loTask.Status == "Done"
    MESSAGEBOX(loTask.Result)
ENDIF
IF loTask.Status == "Failed"
    MESSAGEBOX("Error: " + loTask.Error)
ENDIF
* Progress bar update:
oProgressBar.Value = loTask.Progress

5. Wait for the result (blocking)

WaitFor() blocks execution until the task finishes or the timeout elapses. Use this in scripts, tests or batch processes, never in a UI event.

* Wait up to 5 seconds (5000 ms)
loCore.WaitFor(loTask, 5000)
IF loTask.Status == "Done"
    ? loTask.Result
ELSE
    ? "Task did not complete in time: " + loTask.Status
ENDIF
Warning: Never call WaitFor() inside a UI event handler (Click, Timer, etc.) -- it will freeze VFP. Use it only in background scripts or tests.

Next: Advanced Patterns -- API Reference