Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed execution in Delphi

Is it possible to start procedure delayed after the calling procedure will end?

procedure StartLoop;
begin
  DoSomething;
end;

procedure FormCreate(...);
begin
  if ParamStr(1)='start' then StartLoop;
end;

StartLoop will be called inside FormCreate, and FormCreate will be waiting, and block further execution not only the of FormCreate itself, but also further procedures executing after it (FormShow, etc.), and form will not show until StartLoop will end.

I need to wait until FormCreate will end, and run StartLoop after that (without using threads).

like image 734
jsmith Avatar asked Nov 29 '22 21:11

jsmith


1 Answers

If you are using 10.2 Tokyo or later, you can use TThread.ForceQueue():

procedure TMyForm.FormCreate(Sender: TObject);
begin
  if ParamStr(1) = 'start' then
    TThread.ForceQueue(nil, StartLoop);
end;

Otherwise, you can use PostMessage() instead:

const
  WM_STARTLOOP = WM_USER + 1;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  if ParamStr(1) = 'start' then
    PostMessage(Handle, WM_STARTLOOP, 0, 0);
end;

procedure TMyForm.WndProc(var Message: TMessage);
begin
  if Message.Msg = WM_STARTLOOP then
    StartLoop
  else
    inherited;
end;
like image 188
Remy Lebeau Avatar answered Dec 05 '22 13:12

Remy Lebeau