如何编写idtcpclient接收数据的线程
第一次写线程,查了些资料但是写出来总是报错。
头文件
class TClientThread : public TThread{ private: void __fastcall Execute(); public: __fastcall TClientThread(void);};
TClientThread *ClientThread;//---------------------------------------__fastcall TClientThread::TClientThread(void) : TThread(true){ FreeOnTerminate = true; Resume();}//---------------------------------------void __fastcall TClientThread::Execute(){ while(!Terminated) { if(!MainForm->IdTCPClient1->Connected()) { Terminate(); } else { try { AnsiString receiveMessage = MainForm->IdTCPClient1->ReadLn(); //读取数据然后在reditMessage显示 MainForm->reditMessage->Lines->Add(receiveMessage); } catch(...) { MainForm->reditMessage->Lines->Add("Err"); } } }}//-------------------------------------void __fastcall TMainForm::IdTCPClient1Connected(TObject *Sender) { ClientThread = new TClientThread; //连接后创建线程}//-------------------------------------void __fastcall TMainForm::IdTCPClient1Disconnected(TObject *Sender){ ClientThread-> Terminate(); //断开后释放线程 IdTCPClient1->Disconnect();}//---------------------------------------