0% found this document useful (0 votes)
70 views2 pages

($ifdef Tracedebug) ($ENDIF) : // - Use Whatever You Want..

This document contains code for creating a TCP/IP connection and reading data from that connection. It defines a procedure that creates a thread to handle reading from the TCP/IP client. The reading thread connects to the client, reads data using a timeout, and synchronizes any received data to a handler. On form close, it terminates the reading thread, waits for it to finish, and frees its resources.

Uploaded by

didier_o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views2 pages

($ifdef Tracedebug) ($ENDIF) : // - Use Whatever You Want..

This document contains code for creating a TCP/IP connection and reading data from that connection. It defines a procedure that creates a thread to handle reading from the TCP/IP client. The reading thread connects to the client, reads data using a timeout, and synchronizes any received data to a handler. On form close, it terminates the reading thread, waits for it to finish, and frees its resources.

Uploaded by

didier_o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 procedure TMainForm.

CreateTCPIPConnection;
2 begin
3 if not Assigned(ZPReadThread) then
4 begin
5 IdTCPClient.Host := AddressEdit.Text;
6 IdTCPClient.Port := StrToIntDef(PortEdit.Text, 4769);
7 try
8 ZPReadThread := TReadingThread.Create(IdTCPClient);
9 try
10 ZPReadThread.OnData := DataReceived;
11 ZPReadThread.Start;
12 except
13 FreeAndNil(ZPReadThread);
14 raise;
15 end;
16 except
17 on E: Exception do
18 begin
19 {$IFDEF TRACEDEBUG}AddDebugEntry('TCP/IP exception creating read thread :
'+E.Message);{$ENDIF}
20 end;
21 end;
22 end;
23 end;
24
25 procedure TReadingThread.Execute;
26 begin
27 {$IFDEF TRACEDEBUG}AddDebugEntry('Read thread created');{$ENDIF}
28
29 try
30 FClient.ConnectTimeout := 10000; // <-- use whatever you want...
31 FClient.Connect;
32 except
33 on E: Exception do
34 begin
35 {$IFDEF TRACEDEBUG}AddDebugEntry('TCP/IP connect exception :
'+E.Message);{$ENDIF}
36 raise;
37 end;
38 end;
39
40 try
41 FClient.IOHandler.ReadTimeout := 5000; // <-- use whatever you want...
42 while not Terminated do
43 begin
44 {$IFDEF TRACEDEBUG}AddDebugEntry('Read thread ReadLn (before)');{$ENDIF}
45 try
46 FData := FClient.IOHandler.ReadLn;
47 except
48 on E: Exception do
49 begin
50 {$IFDEF TRACEDEBUG}AddDebugEntry('TCP/IP IOHandler.ReadLn exception :
'+E.Message);{$ENDIF}
51 raise;
52 end;
53 end;
54 //FClient.IOHandler.ReadBytes(AData, sizeof(TWaveFormSample), False);
55 {$IFDEF TRACEDEBUG}AddDebugEntry('Read thread ReadLn (after)');{$ENDIF}
56 if (FData <> '') and Assigned(FOnData) then
57 Synchronize(DataReceived);
58 //Sleep(1);
59 end;
60 finally
61 FClient.Disconnect;
62 end;
63 end;
64
65 procedure TReadingThread.DoTerminate;
66 begin
67 {$IFDEF TRACEDEBUG}AddDebugEntry('Read thread terminating');{$ENDIF}
68 inherited;
69 end;
70
71 procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
72 begin
73 if Assigned(ZPReadThread) then
74 begin
75 {$IFDEF TRACEDEBUG}AddDebugEntry('Terminating Read thread');{$ENDIF}
76 ZPReadThread.Terminate;
77 {$IFDEF TRACEDEBUG}AddDebugEntry('Waiting for read thread termination');{$ENDIF}
78 ZPReadThread.WaitFor;
79 {$IFDEF TRACEDEBUG}AddDebugEntry('Finished waiting for read thread
termination');{$ENDIF}
80 FreeAndNil(ZPReadThread);
81 end;
82 end;

You might also like