A synchronization problem of multi-threaded Dll,ocx in Delphi writing

Source: Internet
Author: User
Tags message queue

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.