Delphi Parallel.For creating objects

78 Views Asked by At

I'm trying to create a list of frames using parallelism to improve speed (with Parallel.For()).

The problem is that my code just don't run after this statement:

lFrameItem := TFrameItemEntrega.Create(nil);

Would I be expecting a lot of ease from Delphi with this function? I suspect this, because from what I've worked with threads, it's not that smooth.

What kind of function do I need to use to make this work?

I added debug messages to check where the code was stopping.

My TFrameItemEntrega doesn't have a personalized Creator, it's the standard TCustomFrame Creator.

Delphi Rio 10.3

procedure TfrmControleItensEntrega.CarregarFramesItensNota;
var
  lFrame: TFrameItemEntrega;
  lVenda: TVenda;
  Sync: TMultiReadExclusiveWriteSynchronizer;
begin
  if TVenda.ExisteVenda(FNota) then
  begin
    lVenda := TVenda.Create;
    try
      lVenda.Nota := FNota;
      lVenda.CarregarItens := True;
      lVenda.CarregaVenda;

      FListaFramesItemLocal := TObjectList<TFrameItemEntrega>.Create;

      Sync := TMultiReadExclusiveWriteSynchronizer.Create;
      try
        TParallel.for (0, Pred(lVenda.ListaDeItens.Count),
          procedure(I: Integer)
          var
            lFrameItem: TFrameItemEntrega;
          begin
            try
              OutputDebugString(pchar('enter'));
              lFrameItem := TFrameItemEntrega.Create(nil);
              OutputDebugString(pchar('exit'));
              lFrameItem.AtualizarFrame(lVenda.ListaDeItens[I].Produto.Codigo, lVenda.Cliente.Codigo,
                lVenda.ListaDeItens[I].Quantidade);
            except
              on E: Exception do
              begin
                OutputDebugString(pchar(e.Message));
              end;
            end;

            Sync.BeginWrite;
            try
              FListaFramesItemLocal.Add(lFrameItem);
            finally
              Sync.EndWrite;
            end;
          end);
      finally
        Sync.Free;
      end;

      if FListaFramesItemLocal.Count > 0 then
      begin
        for lFrame in FListaFramesItemLocal do
        begin
          lFrame.Align := alBottom;
          lFrame.Parent := sbxItens;
          lFrame.Align := alTop;
        end;
      end;
    finally
      lVenda.Free;
    end;
  end;
end;
0

There are 0 best solutions below