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

本例用改进后的 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. Redis 数据库的安装

    安装redis真的是简单到不行了,三条命令就可以完成. 1.下载redis: 001.http://redis.io/ 002.也可以在我的网盘下载https://yunpan.cn/crSUX6aS ...

  2. SQL中 and or优先级问题(转)

    刚刚在项目中遇到这样一个问题,SQL语句如下: select * from LOAN_BACK_LIBRARY where LIBRARY_ID=1 or LIB_ID=1 and STATUS=3 ...

  3. codevs2822 爱在心中

      2822 爱在心中 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无 ...

  4. 甲骨文推动Java进军“物联网”

    该公司希望在嵌入式设备开发项目上Java可以取代C     随着周二宣布对嵌入式的Java版本进行升级,甲骨文希望扩展该平台到新一代连接设备,又名物联网.甲骨文还希望,Java可以在一些嵌入式开发项目 ...

  5. javacript 面向对象

    1.对象 使用Object创建对象 var p = new Object(); p.name = 'jason'; p.sayName = function(){ alert(this.name); ...

  6. 定义file input

    <div class="inputFileWrapper"> <label for="inputFile"> <input typ ...

  7. asp.net中,我们使用ashx获取数据列表,在前端使用$.ajax()解析

    一直在想在asp.net中怎么才能向在java中那样用struts那样做页面请求. 当然asp.net mvc就是类似struts的东西吧,不过还没来得及学习. 今天就用ashx来接收页面请求,并调用 ...

  8. C++ STL源代码学习(map,set内部heap篇)

    stl_heap.h ///STL中使用的是大顶堆 /// Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap ...

  9. ORACLE Recyclebin管理及flashback recyclebin中的对象

    Flashback用于恢复用户误删除的对象(包括表,索引等), 不支持sys用户. system表空间下的对象,也不能从回收站里拿到.故使用SYS 或者SYSTEM用户登陆时, show recycl ...

  10. java SecurityManager

    ---- 众所周知,Java语言具有完善的安全框架,从编程语言,编译器.解释程序到Java虚拟机,都能确保Java系统不被无效的代码或敌对的编译器暗中破坏,基本上,它们保证了Java代码按预定的规则运 ...