Delphi How to wait for socket answer inside procedure?

Posted by Astronavigator on Stack Overflow See other posts from Stack Overflow or by Astronavigator
Published on 2012-07-02T13:46:25Z Indexed on 2012/07/04 9:16 UTC
Read the original article Hit count: 335

Filed under:
|
|

For some specific needs i need to create procedure that waits for socket request (or answer) in dll:

TForm1 = class(TForm)
  ServerSocket1: TServerSocket;

......

procedure MyWaitProc; stdcall;
begin
  Go := false;
  while not Go do
  begin
    // Wating...
    // Application.ProcessMessages;     // Works with this line
  end;
end;


procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  MessageBoxA(0, PAnsiChar('Received: '+Socket.ReceiveText), '', MB_OK);
  Go := true;
end;

exports
  MyWaitProc;

When I call Application.ProcessMessages everything works fine: application waits for request and then continues. But in my case calling Application.ProcessMessages causes to unlocking main form on host application (not dll's one). When I don't call Application.ProcessMessages application just hangs couse it cannot handle message...

So, how to create such a procedure that's wating for socket answer ? Maybe there a way to wait for socket answer without using Application.ProcessMessages ?

EDIT

I also tried to use TIdTCPServer, for some reasons, the result is the same.

TForm1 = class(TForm)
  IdTCPServer1: TIdTCPServer;
.....

procedure MyWaitProc; stdcall;
begin
  Go := false;
  while not Go do
  begin
    // Waiting ...
    // Application.ProcessMessages;
  end;
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
  s: string;
begin
  s := AContext.Connection.Socket.ReadString(1);
  AllText := AllText + s;
  Go := True;
end;

© Stack Overflow or respective owner

Related posts about delphi

Related posts about sockets