// 要做的工作很简单,就是用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.010428710095903737http://money.hinet.net/stock/z00.htm?A=/z/choice.htmunit Unit1;interfaceuses
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.
// 来分析抓取一些数据,代码运行正常。但是由于比较费时,所以想用线程来做,
// 可是改成线程后下面的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.010428710095903737http://money.hinet.net/stock/z00.htm?A=/z/choice.htmunit Unit1;interfaceuses
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.
Sleep(50);
while Doc.ReadyState <> 'complete' do
Application.ProcessMessages;
注释掉
在线程里将Application.ProcessMessages;改为
Sleep(50);
2。注意:有些类不能在线程内正常运行。
我认为你的线程可能有问题,有些对像是线程不安全的.要使用Synchronize