Posted: Sun May 18, 2008 6:02 pm Post subject: Recv. Packets in another Thread (IdTCPClient)
Hello,
Hence the IdTCPClient Socket has no OnRead function I need to recvice packets in an endless loop.
But this will of course freeze my program if I would do it in the main-thread.
So I have to create another.
I never worked with threads before and wanted to try it out now!
So I used BeginThread() on the Form.OnCreate procedure.
Here is my thread-procedure:
Code:
procedure test();
var
packet: TPacket;
begin
WaitForInputIdle(0, 10000);
Form1.Memo1.Lines.Add('a');
repeat
if not Form1.Client.Socket.Connected then exit;
try
Form1.Client.Socket.Recv(packet, sizeof(packet));
case packet.header of
01: Form1.LoginPacket(packet);
end;
except
Form1.Memo1.Lines.Add('.');
end;
until
cclose = true;
end;
But as soon as the program started I got erros.
So I googled and found out that threads are VCL-unsafe.
No I'm wondering how I could slove this problem, any ideas?
Use the TThread class.
If it's not a VCL-based form, have it process recv in the idle time of the message pump. There's probably a way to have that situated in the huge list of event handlers for delphi, but I'm not so in-tune with it anymore.
Thanks Renko
I've found a TUT about it and I got it working.
But if I close the client while I'm running it in debugging-mode in delphi I'm getting weird errors because of the thread. (Some random adress-violations).
Why? (Source is attached)
The Extension 'rar' was deactivated by an board admin, therefore this Attachment is not displayed.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum