?
Solved

thread messagepump

Posted on 2003-03-25
6
Medium Priority
?
510 Views
Last Modified: 2007-12-19
This should be easy... but I can't get it working :(

mainform and a thread named clientsocket.

This is the execute loop from the thread:

while (ClientSocket.Connected) and (not Terminated) do
  try
    while {something} do
      DispatchRecord;
    SendRecord;
    SleepEx(1,true);
 except
   on E: EIdReadTimeout do SendRecord;
 end;

SendRecord does not much more then this:
  While PeekMessage(MSg,0,0,0,PM_REMOVE) do
    {1: use message to send something)

also tried: PeekMessage(MSg,INVALID_HANDLE_VALUE,0,0,PM_REMOVE)

But I never get to point 1 !! The message is sent wrongly or the peekmessage loop is not OK.

Here is how I sent a message to the thread:
    PostThreadMessage(ClientSocket.Handle,WM_Client_Data,integer(DataOut),0);
also tried:
    PostMessage(ClientSocket.Handle,WM_Client_Data,integer(DataOut),0);


I think it's something easy I just forgot. what am I doing wrong?

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Assisted Solution

by:jsmugen
jsmugen earned 248 total points
ID: 8207816
you have to create a message queue before you try to getmessage or peekmessage...
try this

PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
while (ClientSocket.Connected) and (not Terminated) do
 try
   while {something} do
     DispatchRecord;
   SendRecord;
   SleepEx(1,true);
except
  on E: EIdReadTimeout do SendRecord;
end;

you have to use PostThreadmessage instead of postmessage.
0
 

Author Comment

by:TicketToTotalitarianism
ID: 8209288
I tried
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
as first thing in the thread.execute function.

and with PostThreadMessage..

but no succes :(

Can somebody give me a working small example? I can't find it on the net.
0
 
LVL 3

Accepted Solution

by:
emadat earned 252 total points
ID: 8210909
I had the same problem and I'm currently considering this solution.

In your TThread class declaration add this:

//==========================================================
  MyThread = class(TServerClientThread)
  private
     FHandle: HWnd;
     ...
     ...
  protected
     procedure WndProc(var Message: TMessage);
     ...
     ...
//==========================================================

Your Execute method should look something like this:

//==========================================================
procedure MyThread.ClientExecute;
var
     ...
     ...
begin
     FHandle := AllocateHWND(WndProc);
     ...
     ...
     while (ClientSocket.Connected) and (not Terminated) do
          try
          while {something} do DispatchRecord;
               SendRecord;
               SleepEx(1,true);
          except
               on E: EIdReadTimeout do SendRecord;
          end;
     ...
     ...
     DeAllocateHWND(FHandle);
end;
//==========================================================

Whenever you need to send a message; use SendMessage or whatever to FHandle:
SendMessage(FHandle, CM_YourMessage, ..., ...);

Regards
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:TicketToTotalitarianism
ID: 8211925
I've found the error..
PostThreadMessage needs a THREADID !.. not a ThreadHandle.
I knew it was something easy.. :D

Emadat, You're creating a window-handle with your thread, so that thread is the owner of it. When you postmessage to that handle, windows sends it to the wndproc of the owner of the window-handle, in this case your thread.

But I have a loop which doesn't only handles messageges.

This is a working example of a thread-messagepump and how to send messages to it.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,unit2, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    test : TTest;// the test-thread
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  test := TTest.Create(false);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PostThreadMessage(test.ThreadID,WM_USER,0,0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  test.Terminate;
end;

end.

// the thread-class
unit Unit2;

interface

uses
  Classes,windows,messages,Dialogs;

type
  TTest = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;

implementation


procedure TTest.Execute;
var
  Msg : Tmsg;
begin
  FreeOnTerminate := true;
  while not terminated do
    While PeekMessage(MSg,0,0,0,PM_REMOVE) do
      messagebeep(1);
  messagebeep(1);
end;

end.


0
 

Expert Comment

by:CleanupPing
ID: 9316606
TicketToTotalitarianism:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 

Author Comment

by:TicketToTotalitarianism
ID: 9449774
thanx for your time!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question