在线等待, 为啥放进线程中就不行了?
// 要做的工作很简单,就是用IdHttp打开一个网页,然后用Doc(IHTMLDocument2)
// 来分析抓取一些数据,代码运行正常。但是由于比较费时,所以想用线程来做,
// 可是改成线程后下面的Application.ProcessMessages老是跳不出循环
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
s := idhttp1.Get('http://www.baidu.com'); // 随便举个例子
WB_LoadHTML(Doc, s);
// 把
while Doc.ReadyState <> 'complete' do
Application.ProcessMessages;
//--------------> 假如线程中能走到此处就算解决了
// ParseHtml;
end;
整个程序的代码简化如下:
http://www.justlamps.us.com/
http://tw.stock.yahoo.com/s/list.php?c=tse&rr=11629791662960.36638015953042296
http://tw.stock.yahoo.com/s/list.php?c=otc&rr=11665957725150.010428710095903737
http://money.hinet.net/stock/z00.htm?A=/z/choice.htm
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, MsHtml;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Doc: IHTMLDocument2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses activex, StrUtils, ComObj;
procedure WB_LoadHTML(IDoc: IHtmlDocument2; HTMLCode: string);
var
sl: TStringList;
ms: TMemoryStream;
begin
sl := TStringList.Create;
try
ms := TMemoryStream.Create;
try
sl.Text := HTMLCode;
sl.SaveToStream(ms);
ms.Seek(0, 0);
(IDoc as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
finally
ms.Free;
end;
finally
sl.Free;
end;
end;
// 要做的工作很简单,就是用IdHttp打开一个网页,然后用Doc(IHTMLDocument2)
// 来分析抓取一些数据,代码运行正常。但是由于比较费时,所以想用线程来做,
// 可是改成线程后下面的Application.ProcessMessages老是跳不出循环
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
s := idhttp1.Get('http://www.baidu.com'); // 随便举个例子
WB_LoadHTML(Doc, s);
// 把
while Doc.ReadyState <> 'complete' do
Application.ProcessMessages;
//--------------> 假如线程中能走到此处就算解决了
// ParseHtml;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Doc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;
end;
end;
end.