How to access instances of the same form from a thread

26 views
Skip to first unread message

Alexandre Pires Alpires

unread,
Oct 13, 2020, 7:49:03 PM10/13/20
to

I Guys, a need a help
I have a form that I instantiate several times, in each instance I start a thread, I would like to know how, from the thread, to access a component in the form that started the specific thread.

form1 (intance 1) - start the thread TReadBase
form1 (instance 2) - start the thread TReadBase

how to know which form instantiated the thread since they have the same name?

I am using Delphi 10.3

Thanks for some help
Alexandre

Reşat ARIKAN

unread,
Oct 17, 2020, 4:41:32 PM10/17/20
to
14 Ekim 2020 Çarşamba tarihinde saat 02:49:03 UTC+3 itibarıyla alexandre...@gmail.com şunları yazdı:
You can extend Tthread to your own thread and set a property named ownerForm: TForm or any object you want.and can set when create your own thread.

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.IOUtils, System.Types;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyThread= class(TThread)
private
FOwnerForm: TForm;
protected
public
constructor Create(ownerForm: TForm);
destructor Destroy; override;
procedure Execute(); override;
published
end;
var
Form1: TForm1;

implementation

{$R *.dfm}

{ TMyThread }

constructor TMyThread.Create(ownerForm: TForm);
begin
inherited Create;
FOwnerForm:= ownerForm;
Self.FreeOnTerminate := True;
end;

destructor TMyThread.Destroy;
begin
inherited;
end;

procedure TMyThread.Execute;
begin
try
fownerForm.buttonX.Click();
except on E: Exception do
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var MT: TMyThread;
begin
MT := TMyThread.Create(Self);
end;

end.

Alexandre Pires Alpires

unread,
Oct 20, 2020, 3:19:52 PM10/20/20
to
Thanks, but fownerForm cant indentifier Button, In my case Button1.
Error: Undeclared identifier: Button1

Some idea?

Reply all
Reply to author
Forward
0 new messages