6

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.

user2091150
  • 978
  • 12
  • 25

1 Answers1

8

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • TThread.Synchronize requires TThread object, but callback just procedure with different thread id. – user2091150 Apr 06 '13 at 21:30
  • 1
    Not so. The methods I refer to are class methods and so do not need an instance. – David Heffernan Apr 06 '13 at 21:34
  • 3
    [DCC Error] UnitMain.pas(130): E2389 Protected member 'TThread.Synchronize' is inaccessible here (Delphi XE2) on `TThread.Synchronize(qt.SendEvent)` – user2091150 Apr 06 '13 at 21:39
  • 6
    You need the two parameter version and pass nil as the first param. – David Heffernan Apr 06 '13 at 21:42
  • It never able to execute function and never able to execute line 12181 `TMonitor.Exit(ThreadLock);` in System.Classes after `TMonitor.Wait(SyncProcPtr.Signal, ThreadLock, INFINITE)` when I trying to do that in dll. – user2091150 Apr 06 '13 at 21:51
  • In a DLL you need to call CheckSynchronize regularly to service Synchronize/Queue http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.CheckSynchronize – David Heffernan Apr 06 '13 at 21:56
  • Thanks for suggestions but nothing works yet, I updated my post. – user2091150 Apr 07 '13 at 12:58
  • The question has completely morphed now. I'm confident I answered the original question. The nitty gritty of Lua I know nothing about. – David Heffernan Apr 07 '13 at 13:03