agentleFS
adrianosantostreinaadrianosantostreina/delphi-dev/skills/delphi-async/SKILL.md

delphi-async

Skill87 starsChanged 5 months agoMITView on GitHub

Especialista em programação assíncrona Delphi. Auto-ativa quando detectar: TTask, TParallel, IFuture, TThread, Synchronize, Queue, TMonitor, TEvent, TCriticalSection, "thread", "assíncrono", "background", "worker", "parallel".

The file

Files
SKILL.md

Delphi Async

Idioma de saída

Detecte o idioma da primeira mensagem. Padrão: pt-BR. Respeite overrides explícitos.

Referência completa

Consulte knowledge/core/delphi-async.md para padrões detalhados.

Padrão TTask (preferido sobre TThread)

procedure TRelatorioService.GerarRelatorioAsync(
  AOnConcluido: TProc<TRelatorio>);
begin
  TTask.Run(
    procedure
    var
      LRelatorio: TRelatorio;
    begin
      LRelatorio := GerarRelatorio; // trabalho pesado na thread background

      TThread.Synchronize(nil, // retorna para a thread principal
        procedure
        begin
          AOnConcluido(LRelatorio);
        end);
    end);
end;

Synchronize vs Queue

SynchronizeQueue
Bloqueia?Sim — aguarda a UI executarNão — dispara e esquece
Quando usarPreciso do resultado na UI antes de continuarNotificação, atualização de progresso

Cancelamento com TTask

var LTask: ITask;
LTask := TTask.Run(
  procedure
  begin
    while not (LTask.Status = TTaskStatus.Canceled) do
    begin
      // trabalho...
      TTask.CurrentTask.CheckCanceled; // lança EOperationCancelled se cancelado
    end;
  end);

// Para cancelar:
LTask.Cancel;

Regras

  • Nunca acessar VCL/FMX fora da thread principal — sempre via Synchronize ou Queue
  • TCriticalSection para proteção de recursos compartilhados — sempre try/finally
  • TMonitor para sincronização mais granular (Delphi XE2+)
  • IFuture<T> quando precisa do resultado da task

Armadilha: captura de variáveis locais

// ERRADO — LId é capturado por referência, pode ter mudado quando a task roda
for LId in LIds do
  TTask.Run(procedure begin ProcessarId(LId); end);

// CORRETO — captura por valor via variável local
for LId in LIds do
begin
  LIdLocal := LId; // cópia local
  TTask.Run(procedure begin ProcessarId(LIdLocal); end);
end;

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.