//Indy元件的確是很好用,可是 IdHTTP的ReadTimeOut事件常不靈驗,導致整個程式因為IdHTTP被卡住(han)
//希望寫一個程式能在想要的時候開啟它,將之加入一個多執行緒中,
//然後用他去get想要的網頁,如此就不會卡住,而影響主程式的程序
//本範例還有引介一個重要的概念-如何去動態產生一個BCB元件,並overwrite他的事件
//依照此範例,你也可以動態產生一個按紐,然後自行定義按鈕的onclick()事件
class TMyThread:public TThread
{
public:
__fastcall TMyThread(void);
AnsiString URL,Proxy;
int Port;
bool ToGet;
private:
AnsiString ss;
TIdHTTP *IdHTTPn;
void __fastcall IdHTTPnStatus(TObject *ASender,
const TIdStatus AStatus, const AnsiString AStatusText);
void __fastcall Execute(void);
};
_fastcall TMyThread::TMyThread(void):TThread(true)
{
ToGet = false;
FreeOnTerminate=true;
Resume();
}
void __fastcall TMyThread::Execute()
{
while(!ToGet){;}
Form_Main->Memo_Message->Lines->Append("Get URL="+URL+" From "+Proxy+":"+IntToStr(Port));
IdHTTPn = new TIdHTTP(0);
IdHTTPn->HandleRedirects = true;
IdHTTPn->ReadTimeout = 20000;
IdHTTPn->ProxyParams->ProxyServer=Proxy;
IdHTTPn->ProxyParams->ProxyPort=Port;
IdHTTPn->OnStatus = IdHTTPnStatus;
//function point 這是這個程式最重要的部份,運用此觀念可以自行定義元件事件
try
{
ss = IdHTTPn->Get(URL);
}
catch(Exception &e)
{
Form_Main->Memo_Message->Lines->Append(e.Message);
}
if(IdHTTPn->ResponseCode==200) Form_Main->Memo_Message->Lines->Append("Sucess");
delete IdHTTPn;
Form_Main->Memo_Message->Lines->Append("================================");
}
void __fastcall TMyThread::IdHTTPnStatus(TObject *ASender,
const TIdStatus AStatus, const AnsiString AStatusText)
{
Form_Main->Memo_Message->Lines->Append("AStatusText="+AStatusText);
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::ToolButton3Click(TObject *Sender)
{//TThread test
TMyThread *MyThread=new TMyThread();
MyThread->URL=("http://dreamyeh.pixnet.net/blog");
//MyThread->Proxy=("proxy.hinet.net");
//MyThread->Port=80;
MyThread->ToGet=true;
}
//---------------------------------------------------------------------------
沒有留言:
張貼留言