Goog morning,
I've a doubt about thread in lazarus: I create a simple thread for blinking a tlabel in my main form:
TBlinker = class(TThread)
private
ftLabel: TLabel;
procedure setComponentLabel(AValue: TLabel);
protected
procedure Execute; override;
procedure reverseBlink;
public
ctrVar: Boolean;
threadRun: Boolean;
constructor Create(CreateSuspended: boolean);
property setLabel: TLabel read ftLabel write setComponentLabel;
procedure setVisibilityOff;
end;
procedure TBlinker.setComponentLabel(AValue: TLabel);
begin
if ftLabel=AValue then Exit;
ftLabel:=AValue;
end;
procedure TBlinker.Execute;
var
i: Integer;
begin
while not Terminated and (ctrVar = true) do
begin
Synchronize(@reverseBlink);
Sleep(200);
end;
setVisibilityOff;
threadRun := false;
end;
procedure TBlinker.reverseBlink;
begin
ftLabel.Visible:= not ftLabel.Visible;
end;
constructor TBlinker.Create(CreateSuspended: boolean);
begin
FreeOnTerminate := True;
threadRun := true;
inherited Create(CreateSuspended);
ctrVar:=true;
end;
procedure TBlinker.setVisibilityOff;
begin
ftLabel.Visible:= False;
end;
Well, now i create two variable on TForm1 as
bp: TBlinker; pnt_bp : ^TBlinker;
and two Metohd for start and stop thread:
function TForm1.createBlinker: Boolean;
//var
// bp : TBlinker;
begin
try
bp := TBlinker.Create(true);
bp.setComponentLabel(Label1);
bp.FreeOnTerminate:= true;
bp.Start;
pnt_bp := Addr(bp);
finally
end;
end;
function TForm1.stopBlinker: Boolean;
begin
try
if (bp=nil) then
Exit;
bp.ctrVar:=false;
bp.Terminate;
bp := nil;
except
on e: Exception do
ShowMessage(e.Message);
end;
end;
And works fine, but;
if I set a local variable instead a public variable bp and I add the code ( as in the comment)
on createBlinker
and i stop Thread with command
TBlinker(pnt_bp^).ctrVar:= false;
I receive SIGSEV error:
why my pointer don't point correctly?