Delphi writes the Dll,ocx if Tthread.synchronze (Proc) is used, which can cause thread deadlock, because the main thread in the EXE cannot be awakened,
Synchronze does not enter the EXE main thread message queue.
The following program automatically solves this problem by joining the Dll,ocx project file and using Tthread.synchronze (PROC) in Dll,ocx without writing a single line of code.
//resolves a problem when a thread in a Dll,ocx file in a Delphi compilation is called Tthread.synchronize and hangs no longer activated//all projects that call the Tthread.synchronize function please include this file//just include this unit in the project fileUnitLib.Common.DLLThread;InterfaceImplementationusesClasses, Windows, Messages;type {Tdllsystemcontroller}Tdllsystemcontroller=class PrivateFhandle:hwnd; Fprevwakemainthread:tnotifyevent; procedureWakemainthread (Sender:tobject); procedureHooksynchronizewakeup; procedureUnhooksynchronizewakeup; protected procedureWndProc (varmessage:tmessage); Public ConstructorCreate; destructorDestroy;Override; End;varFdllcontroller:tdllsystemcontroller;{Tdllsystemcontroller}Constructortdllsystemcontroller.create;begin inherited; ifIslibrary Then beginFhandle:=Allocatehwnd (WNDPROC); Hooksynchronizewakeup; End;End;destructorTdllsystemcontroller.destroy;begin ifIslibrary Then beginDeallocatehwnd (Fhandle); Unhooksynchronizewakeup; End; inherited;End;procedureTdllsystemcontroller.wndproc (varmessage:tmessage);begin CaseMessage.msg ofwm_null:checksynchronize; ElseMessage.result:=DefWindowProc (Fhandle, Message.msg, Message.wparam, Message.lparam); End;End;procedureTdllsystemcontroller.wakemainthread (sender:tobject);beginPostMessage (Fhandle, Wm_null,0,0);End;procedureTdllsystemcontroller.hooksynchronizewakeup;beginFprevwakemainthread:=Classes.wakemainthread; Classes.wakemainthread:=Wakemainthread;End;procedureTdllsystemcontroller.unhooksynchronizewakeup;beginClasses.wakemainthread:=Fprevwakemainthread;End;initialization ifIslibrary ThenFdllcontroller: =tdllsystemcontroller.createElsefdllcontroller:=Nil;Finalization ifAssigned (Fdllcontroller) ThenFdllcontroller.free;End.
A synchronization problem of multi-threaded Dll,ocx in Delphi writing