簡體   English   中英

Delphi FMX中的多線程

[英]MultiThreading in Delphi FMX

我試圖通過TClientDataSet進行迭代,並在Android中刷新TLabelTProgressBar ,但出現此錯誤。 我該如何解決?

錯誤

這是執行過程。 這是我第一次在Delphi中使用多線程,我想知道這一點。

我希望你能幫助我。

procedure TThreadCatalogos.Execute;
    var i : Integer;
        AppPath : string;
    begin
        AppPath := System.IOUtils.TPath.GetPublicPath;
       ProgressBar.Min := 0;

       for i := round(ProgressBar.Min) to round(ProgressBar.Max) do begin
           // check if Self(thread) is terminated, if so exit
           if Terminated then
              Exit;
           Position := i;

           {*******************************************}

            Conexion.Open;
            //CLIENTES
            dsClientes.Open;
            //mtClientes.EmptyDataSet;
            dsClientes.First;
            ProgressBar.Max := dsClientes.RecordCount;
            while not dsClientes.Eof do
            begin
              if not mtClientes.Locate('nombre',dsClientes.FieldByName('nombre').AsString,[]) then
              begin
                Synchronize(procedure()
                begin
                  mtClientes.Insert;
                  mtClientes.Fields[0].Value := dsClientes.FieldByName('cliente_id').Asinteger;
                  mtClientes.Fields[1].Value := dsClientes.FieldByName('nombre').AsString;
                  mtClientes.Fields[2].Value := dsClientes.FieldByName('tipo').AsString;
                  mtClientes.Post;
                  mtClientes.SaveToFile(System.IOUtils.TPath.combine(AppPath,'CLIENTES.bin'),sfBinary);
                  lbl.Text := 'Cliente '+floattostr(ProgressBar.Value)+' de '+floattostr(ProgressBar.Max);
                  ProgressBar.Value := ProgressBar.Value + 1;
                 end);
              dsClientes.Next;
            end;
            //mtClientes.SaveToFile(System.IOUtils.TPath.combine(AppPath,'CLIENTES.xml'),sfXML);
            mtClientes.First;

            end); Exit;

          end;
           {************************************************}

       end;
    end;

要從子線程更新主線程中的進度條,一種方法是:

  1. 使用可原子更新的全局變量,例如您在子線程中更新的32位整數。
  2. 在窗體上使用TTimer事件,該事件基於全局變量中的值更新進度條。

這樣可以防止過於頻繁地更新進度條,並使線程可以非常快速地更新進度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM