0

I'm a beginner with threads in delphi. I wrote this code and it works very well.

It starts with opening ClientDataSet1 until it is completed and then opening ClientDataSet2 and so on.

My question is: how to start opening them at the same time.

TTHread.CreateAnonymousThread(
procedure
begin
  TTHread.Synchronize(nil,
    procedure
    begin
      with ClientDataSet1 do
        try
          ProgressBar1.Max := 2000; // number of records of ClientDataSet1
          PacketRecords := 50;
          Open;
          DisableControls;
          while not Eof do
          begin
            ProgressBar1.Position := ProgressBar1.Position + 1;
            Label1.Caption := ClientDataSet1.RecordCount.ToString;
            Next;
            Application.ProcessMessages;
          end;
          EnableControls;
        except
          // ShowMessage(Msg);
        end;
    end);

  TTHread.Synchronize(nil,
    procedure
    begin
      with ClientDataSet2 do
        try
          ProgressBar2.Max := 2330; // number of records of ClientDataSet2
          PacketRecords := 80;
          Open;
          DisableControls;
          while not Eof do
          begin
            ProgressBar2.Position := ProgressBar2.Position + 1;
            Label2.Caption := ClientDataSet2.RecordCount.ToString;
            Next;
            Application.ProcessMessages;
          end;
          EnableControls;
        except
          // ShowMessage(Msg);
        end;
    end);
end).Start;

Please help me.

Ferrero
  • 21
  • 2
  • 4
    If you are using Application.ProcessMessages in a thread, chances are that you don't understand enough about threads and how to use them. Try reading up on the subject before jumping in with both feet. Hint: If you are trying to have both CDSs retrieving data at the same time, you need two threads, one for each CDS. And NO APMs! (use a semaphore instead). – MartynA Sep 06 '18 at 19:09
  • Means it is a mistake to use Application.ProcessMessages in threads. – Ferrero Sep 06 '18 at 19:34
  • 1
    Yes, it is certainly a mistake/misconception. Start by reading this: http://www.nickhodges.com/MultiThreadingInDelphi/ToC.html – MartynA Sep 06 '18 at 19:37
  • Thank you for your quick answer, I will try read more... – Ferrero Sep 06 '18 at 19:41

0 Answers0