读网页,通常是一个耗时操作。故把读网页放入线程是显得比较重要了。

本例用改进后的 TIdhttpEx 加上线程来实现读网页。

它的父类TSimpleThread 在此

本例程源码在此

源码中包含了所有的支持单元,其它单元后续会慢慢讲解

 unit uReadHtmlThread;

 interface

 uses
uSimpleThread, uIdhttpEx; type TReadHtmlThread = class; // 提前申明 TReadHtmlThread 是一个类,后面好办事 TReadStatus = (rsOK, rsErr); // 这里就用上TReadHtmlThread ,不然要写个 Sender:TObject 用起来不方便
TOnReadStatus = procedure(Sender: TReadHtmlThread; AStatus: TReadStatus) of object; TReadHtmlThread = class(TSimpleThread)
private FIdHttp: TIdhttpEx; // 这是我改进后的 TIdhttp
FOnReadStatus: TOnReadStatus; FUrl: string;
FNewUrl: string;
FHtml: string; FListIndex: integer; // 我工用中用的,请把它当成一个参数即可。
FPosInList: integer; // 同上 procedure InitIdhttp; // 重新创建 FIdhttp, 如果读网页出错了,就用一下它(经验之谈) procedure ReadHtml; // 本例重点,请仔细看
procedure DoReadHtml; // 本例重点,请仔细看 procedure SetOnReadStatus(const Value: TOnReadStatus);
procedure DoOnReadStatus(AStatus: TReadStatus); // 执行事件,关于事件均可参考此写法
procedure SetHtml(const Value: string);
procedure SetUrl(const Value: string);
procedure SetListIndex(const Value: integer);
procedure SetPosInList(const Value: integer); public constructor Create; reintroduce; // 再次把 Create 的参数去掉,为以后线程池做准备
{ 因为线程池会用到泛型的 LIST ,泛型定义时可以写一个约束条件 如:
TSimpleThing<T:TXXObject,Constructor> 这个 Constructor 要求 T 的Create没有参数
}
destructor Destroy; override;
procedure StartThread; override; // 启动线程,注意看!!!
property OnReadStatus: TOnReadStatus read FOnReadStatus write SetOnReadStatus;
property Url: string read FUrl Write SetUrl;
property NewUrl: string read FNewUrl;
property Html: string Read FHtml Write SetHtml;
property ListIndex: integer read FListIndex write SetListIndex;
property PosInList: integer read FPosInList write SetPosInList;
end; implementation { TReadHtmlThread }
uses
uOperateIndy, SysUtils;
{ uOperateIndy 是我写的一个单元,操作Idhttp简便方法 } destructor TReadHtmlThread.Destroy;
begin
WaitThreadStop; // 在父中说了为什么要写这句
if Assigned(FIdHttp) then
FIdHttp.Free;
inherited;
end; procedure TReadHtmlThread.DoOnReadStatus(AStatus: TReadStatus);
begin
if Assigned(FOnReadStatus) then
FOnReadStatus(self, AStatus);
end; procedure TReadHtmlThread.DoReadHtml;
begin // 这才是重点 InitIdhttp; FNewUrl := FUrl;
if IdhttpGet(FIdHttp, FUrl, FHtml) then
begin
FNewUrl := FIdHttp.Url.URI; // 重定向后的 Url
DoOnReadStatus(rsOK)
end
else
DoOnReadStatus(rsErr); end; procedure TReadHtmlThread.InitIdhttp;
begin
if Assigned(FIdHttp) then
begin
FIdHttp.Free;
end;
FIdHttp := TIdhttpEx.Create(nil);
end; procedure TReadHtmlThread.ReadHtml;
begin
ExeProcInThread(DoReadHtml); // 哈哈,就一句!
end; procedure TReadHtmlThread.SetHtml(const Value: string);
begin
FHtml := Value;
end; procedure TReadHtmlThread.SetListIndex(const Value: integer);
begin
FListIndex := Value;
end; procedure TReadHtmlThread.SetOnReadStatus(const Value: TOnReadStatus);
begin
FOnReadStatus := Value;
end; procedure TReadHtmlThread.SetPosInList(const Value: integer);
begin
FPosInList := Value;
end; procedure TReadHtmlThread.SetUrl(const Value: string);
begin
FUrl := Value;
end; procedure TReadHtmlThread.StartThread;
begin
inherited;
ReadHtml; // 其实还是这一句,哈哈
end; constructor TReadHtmlThread.Create;
begin
inherited Create(false);
end; end.

uReadHtmlThread.pas

附:delphi 进阶基础技能说明

delphi 读网页线程TReadHtmlThread的更多相关文章

  1. delphi : 取得网页源码内容

    取得网页的源码内容的函数以及调用方法供大家参考: program geturl; uses wininet, windows; //取网页内容 function StrPas(const Str: P ...

  2. Delphi中的线程类 - TThread详解

    Delphi中的线程类 - TThread详解 2011年06月27日 星期一 20:28 Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本 ...

  3. 在Delphi中创建线程,请一定使用BeginThread()代替CreateThread()创建线程!(更好的管理异常)

    在Delphi中创建线程,请一定使用BeginThread()代替CreateThread()创建线程! 如果直接使用Win32的API函数CreateThread()创建多个线程,也是可以创建的.但 ...

  4. Delphi中的线程类(转)

    Delphi中的线程类 (转) Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对 TThread类的几个成员作一简单介绍,再说明一下 ...

  5. DELPHI读取网页源文件和获取字符串

    说到网页采集,通常大家以为到网上偷数据,然后把到收集到的数据挂到自己网上去.其实也可以将采集到的数据做为公司的参考,或把收集的数据跟自己公司的业务做对比等.目前网页采集多为3P代码为多(3P即ASP. ...

  6. Delphi多线程编程--线程同步的方法(事件、互斥、信号、计时器)简介

    更详细的可以参考:http://www.cnblogs.com/xumenger/p/4450659.html 或者参考之后的博客 四个系统内核对象(事件.互斥.信号.计时器)都是线程同步的手段,从这 ...

  7. 再读C++线程池

    最近仔细看了一下https://github.com/henkel/threadpool代码,总体感觉非常精巧,使用了 boost库的bind function完成了线程池与业务端的完全解耦:所有的任 ...

  8. delphi 16 网页缩放

    网页放大 网页缩小         WebBrowser1.OleObject.Document.Body.Style.Zoom := 0.50; 缩放网页 Ctrl+中键↑ 放大 Ctrl+中键↓ ...

  9. delphi假死线程堵塞解决办法

    Delphi的高效不多说... 俗话说:真正的程序员用C语言,聪明的程序员用Delphi,一点都不假,和C++比它比C++更简单,更容易上手,功能丝毫不逊色C++,比起VB,毫无疑问比VB好多了,重要 ...

随机推荐

  1. nodejs iconfont处理

    做前端优化,iconfont可以替换掉很多图片,减少请求,并有很好的兼容性,颜色大小也有很好的自由度.现在网上已经有很多公开的iconfont供我们使用.但是每个项目有不同的应用场景,网上的并不能满足 ...

  2. Leetcode 100 Same Tree python

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  3. bash 变量使用技巧

  4. MYSQL 注释的 3 方法

    方法 1.单行注释 # 方法 2.单行注释 -- 方法 3.多行注释 /*------*/ ------------------------------------------------------ ...

  5. Oracle EBS-SQL (BOM-19):主BOM与替代BOM互换.sql

    替代BOM与主BOM互相转换 BOM: 1-01-27-211       子件:1-01-27-416  ID:2202 BOM替代项:替代0001   子件: 1-01-26-204   ID:2 ...

  6. Oracle EBS-SQL (PO-13):检查采购物料无一揽子协议价格.sql

    Select        msi.segment1                               物料编码,       msi.DESCRIPTION                 ...

  7. 使用xmanager 远程redhat6.3

    之前装过一次,特别麻烦,装上只有远程还卡卡的,这次按照教程居然装的灰常顺利,不符合我bug体质的特性,一定要记下来啊~~~ 1.先关闭防火墙 # service iptables stop #chkc ...

  8. x/nfu-用gdb查看内存

    用gdb查看内存 2007-12-08 12:43 用gdb查看内存 格式: x /nfu <addr> 说明x 是 examine 的缩写 n表示要显示的内存单元的个数 f表示显示方式, ...

  9. nyoj 1237 最大岛屿(dfs)

    描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王. 这是一个由海洋.岛屿和海盗组 ...

  10. android listiew适配器

    List<Map<String>> Items = new ArrayList<Map<String>>(); // 把该显示的内容放到list中 fo ...