简体   繁体   English

Delphi FMX中的多线程

[英]MultiThreading in Delphi FMX

I'm trying to iterate though a TClientDataSet and refresh a TLabel and a TProgressBar in Android, but I get this error. 我试图通过TClientDataSet进行迭代,并在Android中刷新TLabelTProgressBar ,但出现此错误。 How can I fix it? 我该如何解决?

错误

This is the execute procedure. 这是执行过程。 is the first time i'm working wuth multithreads in Delphi and i would like to know about that. 这是我第一次在Delphi中使用多线程,我想知道这一点。

I hope you can help me. 我希望你能帮助我。

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;

To update a progress bar in a main thread from a child thread one approach is to: 要从子线程更新主线程中的进度条,一种方法是:

  1. Use atomically updateable global variable(s), like a 32 bit integer that you update in the child thread. 使用可原子更新的全局变量,例如您在子线程中更新的32位整数。
  2. Use a TTimer event on the form that updates the progress bar based off the values in the global variable(s). 在窗体上使用TTimer事件,该事件基于全局变量中的值更新进度条。

This prevents updating the progress bar too often and lets the thread do a very quick updates to progress. 这样可以防止过于频繁地更新进度条,并使线程可以非常快速地更新进度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM