Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to wait for TThread instance to start up

Tags:

delphi

tthread

Between time of TThread instance creation and start up, main thread will continue with code execution. If code in main thread depends on thread in question to be fully up and running it has to wait somehow until thread Execute method actually started.

Consider following code:

const
  WM_MY_ACTION = WM_APP + 10;

type
  TWndThread = class(TThread)
  protected
    fWndHandle: THandle;
    IsRunning: boolean;
    procedure WndProc(var Msg: TMessage);
    procedure Execute; override;
  public
    Test: integer;
    procedure AfterConstruction; override;
    procedure DoAction;
  end;

procedure TWndThread.AfterConstruction;
begin
  inherited;
  while not IsRunning do Sleep(100); // wait for thread start up
end;

procedure TWndThread.Execute;
var
  Msg: TMsg;
begin
  fWndHandle := AllocateHWnd(WndProc);
  IsRunning := true;
  try
    while not Terminated do
      begin
        if MsgWaitForMultipleObjects(0, nil^, False, 1000, QS_ALLINPUT) = WAIT_OBJECT_0 then
          begin
            while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
              begin
                TranslateMessage(Msg);
                DispatchMessage(Msg);
              end;
          end;
      end;
  finally
    DeallocateHWnd(fWndHandle);
  end;
end;

procedure TWndThread.WndProc(var Msg: TMessage);
begin
  case Msg.Msg of
    WM_MY_ACTION:
      begin
        inc(Test);
      end;
    else Msg.Result := DefWindowProc(fWndHandle, Msg.Msg, Msg.WParam, Msg.LParam);
  end;
end;

procedure TWndThread.DoAction;
begin
  PostMessage(fWndHandle, WM_MY_ACTION, 0, 0);
end;

var
  t: TWndThread;
begin
  t := TWndThread.Create;
  t.DoAction;
  t.Terminate;
end;

Without loop that waits for IsRunning flag, DoAction will not be able to successfully post message to contained window handle because it will not yet be created. Basically, inc(Test) inside WndProc will not be triggered.

Is there a better way to wait for thread start up and complete necessary initialization inside Execute method or is this solution as good as it gets?

Note: I am aware that AllocateHWnd and DeallocateHWnd are not thread safe and should not be used in production code like above example.

like image 874
Dalija Prasnikar Avatar asked Feb 19 '15 14:02

Dalija Prasnikar


1 Answers

Main thread

  1. Create an event. For instance, TSimpleEvent will suffice for your needs.
  2. Set the event to be non-signaled. For TSimpleEvent that's a call to ResetEvent. I expect that a newly minted TSimpleEvent would be in the non-signaled state, but off the top of my head I cannot remember that detail.
  3. Create the thread, passing the event in the constructor.
  4. Wait for the event to become signaled. For TSimpleEvent that means calling WaitFor.

Worker thread

  1. Make a note of the event passed to the thread's constructor.
  2. At the start of the thread execution, signal the event. For TSimpleEvent that means calling SetEvent.
like image 63
David Heffernan Avatar answered Sep 30 '22 18:09

David Heffernan