user2091150
user2091150

Reputation: 998

How to execute not TThread based thread in main thread?

For example from thread provided by callback from CreateTimerQueueTimer in executable or dll? It is significant to have same thread id as main thread.

procedure TMyMainClass.ExecuteMe(someparam: paramtype);
begin
  {something}
end;

and

procedure TimerCallback(pvContext: pointer; fTimerOrWaitFired: boolean); stdcall;
begin
  {do what ?}
end;

Final update:
All this stuff (TThread.Synchronize, TThread.Queue, PostThreadMessage etc) works through messages. So be sure host application of your dll processing messages while waiting for callback.

Upvotes: 6

Views: 3359

Answers (1)

David Heffernan
David Heffernan

Reputation: 612993

To execute code in the main thread, without access to a TThread instance, call the class methods TThread.Synchronize or TThread.Queue.

If you happen to be using an old Delphi compiler that does not have those methods, then SendMessage or PostMessage with a user defined message are the simplest solution.

Upvotes: 8

Related Questions