unit Unit17;
interface uses
System.SysUtils, System.Classes,
Winapi.Windows,
Vcl.Controls,
Vcl.Forms,
Vcl.StdCtrls,
Vcl.ComCtrls;
type
TMyThread =
class(TThread)
protected var
progressbarHandle: THandle;
protected
procedure Execute();
override;
public
constructor Create(
const progressbarHandle: THandle);
end;
TForm1 =
class(TForm)
ProgressBar1: TProgressBar;
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation uses Winapi.CommCtrl;
{$R *.dfm}
{ TMyThread }
constructor TMyThread.Create(
const progressbarHandle: THandle);
begin
inherited Create(True);
self.progressbarHandle := progressbarHandle;
end;
procedure TMyThread.Execute();
var
percentDone: Integer;
begin
percentDone := 0;
while (
not Terminated)
do
begin
Inc(percentDone, 1);
PostMessage(progressbarHandle, PBM_SETPOS, percentDone, 0);
if (percentDone < 100)
then
TThread.Sleep(100)
else
Terminate();
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
myThread: TMyThread;
begin
myThread := TMyThread.Create(ProgressBar1.Handle);
myThread.FreeOnTerminate := True;
myThread.Start();
end;
end.