I
I
Igor2019-10-10 18:21:26
Delphi
Igor, 2019-10-10 18:21:26

Delphi creating an instance of a class in a thread?

It's hard to formulate the question correctly, but I'll try.
I have a good idea of ​​how everything works.
I work with flows, I manage.
Synchronization.
Problems.
And other errors in multi-threaded design.
This question may seem stupid, a potential mistake can ruin your life, and then suicide.
I will give a code example

threadCard := TCardThread.CreateAnonymousThread(
    procedure
    var
      this: TCardThread;
    begin
      this := TThread.CurrentThread as TCardThread;
      FDQueryUrl := Dm.GetQuery();
      ...
   end;

function TDm.GetQuery: TFDQuery;
begin
  Result := TFDQuery.Create(nil);
  Result.Connection := TFDConnection.Create(nil);
  Result.Connection.Params.Assign(FDConnection.Params);
end;

I consistently create flows, they seem to work correctly.
But sometimes an anomaly occurs, sometimes it is difficult to immediately determine what causes the error.
And I sort of eliminated obvious, gross, errors when using streams.
Notice the line
FDQueryUrl := Dm.GetQuery();
Where is this instance stored now, in the context of the thread or on the stack/heap of the main thread?
Am I instantiating the thread correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question