killercode
killercode

Reputation: 1706

TThread Doesn't Do It's Job Unless there is a MessageBox in the Middle !

i created a class of TThread to do some socket operations, the thing is, the code doesnt work unless i add MessageBox to it, sockets wont work unless i put a MessageBox call before it

 Sleep(2000); //Waiting for the Socket to Come to the Array
 // Messagebox(0, '', '', 0); { Wont work unless this line is Uncommented }
 if Server.ClientList[Handle] <> nil then
 begin
  if (Server.ClientList[Handle].Connected) and (AppSocket.Connected) do
  begin
   // Send Data on Socket
   // Relay Data between Server.ClientList[Handle] and AppSocket;
  end; 

Upvotes: 0

Views: 323

Answers (2)

avra
avra

Reputation: 3730

Try to replace Messagebox() with Application.ProcessMessages and see what happens.

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 595369

Assuming you are using non-blocking sockets, then your thread needs a running message queue and processing loop. That is why calling MessageBox() works - it is a modal dialog that pumps the calling thread's message queue internally. Your thread needs to call PeekMessage() or GetMessage() in a loop for the lifetime of the connection(s). Your loop can use MsgWaitForMultipleObjects() to detect when the message queue has something to process, if your thread has other things it needs to do.

Upvotes: 2

Related Questions