Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs;
- const
- WM_UPDATE_FORM = WM_USER + 1;
- type
- TTaskUpdateEvent = procedure(Sender: TObject; TaskID, Position: Integer) of object;
- TFormUpdater = class
- private
- FWndHandle: HWND;
- FOnTaskUpdate: TTaskUpdateEvent;
- procedure WndProc(var AMessage: TMessage);
- public
- constructor Create;
- destructor Destroy; override;
- // the following event will be processed in the context of the main thread
- property OnTaskUpdate: TTaskUpdateEvent read FOnTaskUpdate write FOnTaskUpdate;
- // the following property is used to get handle which you'll pass to your threads
- // and which will be the recipient of the posted messages; here WM_UPDATE_FORM
- property WndHandle: HWND read FWndHandle;
- end;
- type
- TForm1 = class(TForm)
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- FFormUpdater: TFormUpdater;
- procedure OnTaskUpdate(Sender: TObject; TaskID, Position: Integer);
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- { TFormUpdater }
- constructor TFormUpdater.Create;
- begin
- inherited;
- FWndHandle := AllocateHWnd(WndProc);
- end;
- destructor TFormUpdater.Destroy;
- begin
- if FWndHandle <> 0 then
- DeallocateHWnd(FWndHandle);
- inherited;
- end;
- procedure TFormUpdater.WndProc(var AMessage: TMessage);
- begin
- if AMessage.Msg = WM_UPDATE_FORM then
- try
- if Assigned(FOnTaskUpdate) then
- FOnTaskUpdate(Self, AMessage.LParam, AMessage.WParam);
- except
- Application.HandleException(Self);
- end
- else
- AMessage.Result := DefWindowProc(FWndHandle, AMessage.Msg,
- AMessage.WParam, AMessage.LParam);
- end;
- { TForm1 }
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- FFormUpdater := TFormUpdater.Create;
- FFormUpdater.OnTaskUpdate := OnTaskUpdate;
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- FFormUpdater.Free;
- end;
- procedure TForm1.OnTaskUpdate(Sender: TObject; TaskID, Position: Integer);
- begin
- // do whatever with the progress bars and other controls of the form here you're
- // having here the TaskID and Position parameters passed from the posted message
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment