Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

thread messagepump

Posted on 2003-03-25
6
Medium Priority
?
514 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
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

564 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