TMyThread = class(TThread) strict private FLabel: TLabel; FStopEvent: THandle; strict private procedure UpdateLabel; protected procedure Execute; public constructor Create(ALabel: TLabel); destructor Destroy; override; end; constructor Create(ALabel: TLabel); begin inherited Create(False); FLabel := ALabel; FStopEvent := CreateEvent(nil, False, False, nil); end; destructor TMyThread.Destroy; begin SetEvent(FStopEvent); inherited Destroy; CloseHandle(FStopEvent); end; procedure TMyThread.UpdateLabel; begin if Length(FLabel.Caption) > 10 then FLabel.Caption := '-' else FLabel.Caption := FLabel.Caption + '-'; end; procedure TMyThread.Execute; var LRes: Cardinal; begin repeat LRes := WaitForSingleObject(FStopEvent, 1000); case LRes of WAIT_TIMEOUT: Synchronize(UpdateLabel); WAIT_FAILED: RaiseLastOSError; end; until LRes = WAIT_OBJECT_0; end; procedure TForm1.Button1Click(Sender: TObject); begin if FThread = nil then FThread := TMyThread.Create(Label1) else FreeAndNil(FThread); end;
UpdateSomething in the evening I stopped thinking. There is a much simpler solution.
We throw a timer on the form, specify the Interval property, how often it should work, in the OnTimer handler OnTimer write
procedure TForm1.Timer1Timer(Sender: TObject); begin if Length(Label1.Caption) > 10 then Label1.Caption := '-' else Label1.Caption := Label1.Caption + '-'; end;
In the OnClick handler, OnClick write buttons
procedure TForm1.Button1Click(Sender: TObject); begin Timer1.Enabled := not Timer1.Enabled; end;
-until the line length exceeds a certain length. And after exceeding the line starts to fill again. So? - Anton Shchyrov