Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6649073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:43:52+00:00 2026-05-26T00:43:52+00:00

I have a Delphi 6 application that has a thread dedicated to communicating with

  • 0

I have a Delphi 6 application that has a thread dedicated to communicating with a foreign application that uses SendMessage() and WM_COPYDATA messages to interface with external programs. Therefore, I create a hidden window with AllocateHWND() to service that need since a thread message queue won’t work due to the SendMessage() function only accepting window handles, not thread IDs. What I’m not sure about is what to put in the thread Execute() method.

I assume that if I use a GetMessage() loop or a create a loop with a WaitFor*() function call in it that the thread will block and therefore the thread’s WndProc() will never process the SendMessage() messages from the foreign program right? If so, what is the correct code to put in an Execute() loop that will not consume CPU cycles unnecessarily but will exit once a WM_QUIT message is received? I can always do a loop with a Sleep() if necessary but I’m wondering if there is a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:43:52+00:00Added an answer on May 26, 2026 at 12:43 am

    AllocateHWnd() (more specifically, MakeObjectInstance()) is not thread-safe, so you have to be careful with it. Better to use CreatWindow/Ex() directly instead (or a thread-safe version of AllocateHWnd(), like DSiAllocateHwnd().

    In any case, an HWND is tied to the thread context that creates it, so you have to create and destroy the HWND inside your Execute() method, not in the thread’s constructor/destructor. Also, even though SendMessage() is being used to send the messages to you, they are coming from another process, so they will not be processed by your HWND until its owning thread performs message retrieval operations, so the thread needs its own message loop.

    Your Execute() method should look something like this:

    procedure TMyThread.Execute;
    var
      Message: TMsg;
    begin
      FWnd := ...; // create the HWND and tie it to WndProc()...
      try
        while not Terminated do
        begin
          if MsgWaitForMultipleObjects(0, nil^, False, 1000, QS_ALLINPUT) = WAIT_OBJECT_0 then
          begin
            while PeekMessage(Message, 0, 0, 0, PM_REMOVE) do
            begin
              TranslateMessage(Message);
              DispatchMessage(Message);
            end;
          end;
        end;
      finally
        // destroy FWnd...
      end;
    end;
    
    procedure TMyThread.WndProc(var Message: TMessage);
    begin
      if Message.Msg = WM_COPYDATA then
      begin
        ...
        Message.Result := ...;
      end else
        Message.Result := DefWindowProc(FWnd, Message.Msg, Message.WParam, Message.LParam);
    end;
    

    Alternatively:

    // In Delphi XE2, a virtual TerminatedSet() method was added to TThread,
    // which is called when TThread.Terminate() is called.  In earlier versions,
    // use a custom method instead...
    
    type
      TMyThread = class(TThread)
      private
        procedure Execute; override;
        {$IF RTLVersion >= 23}
        procedure TerminatedSet; override;
        {$IFEND}
      public
        {$IF RTLVersion < 23}
        procedure Terminate; reintroduce;
        {$IFEND}
      end;
    
    procedure TMyThread.Execute;
    var
      Message: TMsg;
    begin
      FWnd := ...; // create the HWND and tie it to WndProc()...
      try
        while not Terminated do
        begin
          if WaitMessage then
          begin
            while PeekMessage(Message, 0, 0, 0, PM_REMOVE) do
            begin
              if Message.Msg = WM_QUIT then Break;
              TranslateMessage(Message);
              DispatchMessage(Message);
            end;
          end;
        end;
      finally
        // destroy FWnd...
      end;
    end;
    
    {$IF RTLVersion < 23}
    procedure TMyThread.Terminate;
    begin
      inherited Terminate;
      PostThreadMessage(ThreadID, WM_QUIT, 0, 0);
    end;
    {$ELSE}
    procedure TMyThread.TerminatedSet;
    begin
      PostThreadMessage(ThreadID, WM_QUIT, 0, 0);
    end;
    {$IFEND}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Delphi 7 application that has two views of a document (e.g.
I have a Delphi 2007 application that has a TAnimate control with a FindFile
There is a thread in my Delphi application that has a message-waiting loop. Every
I have a delphi (Win32) web application that can run either as a CGI
I have a Delphi application similar to Taskbar Shuffle that includes a hook dll.
I have a console application written in Delphi. I saw that I can have
I have a multi-threaded Delphi 6 Pro application that I am currently working on
I have a fairly complex application in Delphi 2006 that communicates through an ApdComport
I have an application (writted using Delphi 2009) that allows a user to run
We have an application that, amongst many other things, has an export to Excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.