I have problem when run a thread in subform.
main form
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Unit2;
procedure TForm1.Button1Click(Sender: TObject);
begin
TForm2.create(form1).ShowModal;
end;
SUBform
type
TMthread=class(Tthread)
protected
procedure execute; override;
end;
type
TForm2 = class(TForm)
Label1: TLabel;
procedure FormShow(Sender: TObject);
private
public
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
uses Unit1;
procedure TMthread.Execute;
begin
synchronize( procedure
begin
sleep(200);
freeonterminate:=true;
sleep(200);
form2.label1.Caption:='beep';
form1.button1.Caption:='beep';
end);
end;
procedure TForm2.FormShow(Sender: TObject);
var Loadcombo2: TMthread;
begin
Loadcombo2:=TMthread.Create(False);
end;
Program
program Project1;
uses
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
I got error in Execute Procedure when trying to access Form2.Label1.caption.
my test:
When I add the subform(Form2) in the Initialize section(last code) the the application runs without error, but doesn't change Label1.caption on the Form2.(Button1.caption on the main form is changed)
When I put exactly the same thread in main form it works without issues.