Yesterday, I finally added one night, it turned out to be like this.
Thread function procedure TTCPClientHandleThread.Execute; begin ...... while not Terminated do begin if FIdTCPClient.Connected then begin try FRxString: = FRxString FIdTCPClient.ReadChar; FEndOfPacket: = False; except if not FEndOfPacket then begin FEndOfPacket: = True; FDataList.data : = Frxstring; synchronize; // is this sentence / / changed to send message // PostMessage (ReceiveHandle, WM_TCPReceiveData, 0, 0); frXString: = '; end; end; end; end; Else Begin Codesite . Ssendmsg ('TtcpClientHandLethread: Unconnected'); SLEEP (1000); end; end;
procedure TTCPClientHandleThread.HandleInput; begin // below the trigger event, then FOnReceiveData which continues to trigger until the completion of the data analysis if Assigned (FOnReceiveData) then FOnReceiveData (self, PChar (FRxString), Length (FRxString)); end; the basic functions of a Threads used to receive data. Indy's blocking method is used. TCP read data is not to give a given length. If the data inside the cache is less than this length, the received is blocked. And the features I need are just to read all the data inside the cache. Didn't find a direct implementation method, so I wrote this thread. Cycling Readchar. If there is data, return data. If there is no data, readchar will block until the timeout, an exception occurs. Set the timeout to 100ms or other data, so you can read all the data over this period of time.
Question Phenomenon: Sometimes it can be received, sometimes it can't, there is no obvious law. I have written a small program used to use this class, there is no problem. There is a problem in this time in the big system. It seems that the first reception is normal, and the previous program can still take a long time. It is not received for the second time. I have been looking for a long time, repeated tracking, I feel that the thread will stop after the first reception. In thinking is Suspend or Terminate, repeatedly reviewing. Finally, finally found, Synchronize (Handleinput)
This sentence calls HandleInput and triggers FonReceiveData, and then triggered until the data is parsed. I have never returned this sentence.
Change to send messages, threads continue to run.