【发布时间】:2015-05-09 09:22:22
【问题描述】:
我的环境:
Windows 7 Pro (32bit)
C++ Builder XE4
我想知道 WriteLn(); 之后的等待。 以下是我的示例代码。
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
UTF8String rcvdStr;
rcvdStr = AContext->Connection->IOHandler->ReadLn(
IndyTextEncoding(TEncoding::UTF8) );
TList *threads;
TIdContext *ac;
threads = IdTCPServer1->Contexts->LockList();
ac = reinterpret_cast<TIdContext *>(threads->Items[0]);
UTF8String sendStr;
sendStr = "send:" + rcvdStr;
ac->Connection->IOHandler->WriteLn(sendStr);
for(int idx=0; idx<10; idx++) {
Sleep(100);
Application->ProcessMessages();
}
ac->Connection->Disconnect();
IdTCPServer1->Contexts->UnlockList();
}
//---------------------------------------------------------------------------
我将 wait (for(int idx=0;...) 放在 WriteLn() 之后,以便在断开连接之前完成发送。但是,我不确定这是否是正确的等待方式。另外我不知道我应该等待多长时间(在这个示例中,我等待 1000 毫秒)。
问:有没有知道WriteLn()完成的函数?
【问题讨论】:
-
附带说明,您应该使用
IndyTextEncoding_UTF8()而不是IndyTextEncoding(TEncoding::UTF8)。 -
另外,
Application->ProcessMessages();永远不应该在工作线程中被调用(OnExecute被调用)。 -
@RemyLebeau 非常感谢您的 cmets。我从来没有注意到这些。
标签: c++builder indy