webowner47
Programmer
I created a thread for continuously reading from the TIdTCPServer, but it blocks all my client form, after a moment will go not responsive.
Maybe the problem with the way i created the thread, anyway here is my code for the thread:
Maybe the problem with the way i created the thread, anyway here is my code for the thread:
Code:
//---------------------------------------------------------------------------
#ifndef ReadingThreadH
#define ReadingThreadH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
//---------------------------------------------------------------------------
class TReadingThread : public TThread
{
private:
protected:
void __fastcall Execute();
public:
__fastcall TReadingThread(bool CreateSuspended);
void __fastcall GetServerResponse();
};
//---------------------------------------------------------------------------
#endif
Code:
//---------------------------------------------------------------------------
#include <System.hpp>
#pragma hdrstop
#include "ReadingThread.h"
#include "MainUnit.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
void __fastcall TReadingThread::GetServerResponse() {
int msgLength;
UnicodeString uReceivedMessage;
if ( ClientMain->IdTCPClient1->Connected() ) {
msgLength = ClientMain->IdTCPClient1->IOHandler->ReadInt32();
uReceivedMessage = ClientMain->IdTCPClient1->IOHandler->ReadString(msgLength);
if ( uReceivedMessage.Length() != 0 ) {
ClientMain->MessageDisplay1->Lines->Add(uReceivedMessage);
}
}
}
__fastcall TReadingThread::TReadingThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TReadingThread::Execute()
{
//---- Place thread code here ----
while ( !Terminated ) {
Synchronize(&GetServerResponse);
}
}
//---------------------------------------------------------------------------