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

本例用改进后的 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. jq插件又来了,模拟select下拉框,支持上下方向键哦

    好久没来了,更新下插件, 这个原理就是利用的 input[type='hidden']和自定义属性data-value捆绑传值操作的,可是设置默认选项,回调等参数,代码不多,比较简单,吼吼 (func ...

  2. Android 开发使用lambda实现< JDK8兼容

    代码精简无疑是每个程序员的目标,简短易读.java 8中的lambda表达式的使用: 4 easy steps Download and install jdk8. Add the following ...

  3. js如何控制css伪元素内容(before,after)

    曾经遇到的问题,在对抗UC浏览器屏蔽需要把内容输出到css 伪元素中输出.有个疑问如何用js控制它.于是在segmentfault提问,如下是对问题的整理: 如何用js控制css伪类after 简单粗 ...

  4. ssh username@10.2.1.23无法连接

    ssh username@10.2.1.23无法连接 经过排查各种openSSH版本等,最终因为两个机器不是一个段(A:255.255.0.0 B:255.255.255.0)造成的. 关于IP需要以 ...

  5. iOS正则表达式的使用

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  6. VB.NET生成Excel,已存在提示框点否时报错

    如题 Exception from HRESULT: 0x800A03EC 最终没有好的解决方案,只好屏蔽掉 Try obook.SaveAs(excelSaveName) Catch ex As S ...

  7. OpenWRT交叉编译

    对于当前不在OpenWRT repository中的软件,如果是用源码形式发布的,那么可以用OpenWRT Buildroot进行交叉编译. 首先编译好Buildroot(一般编译过一次固件,就已经编 ...

  8. android 管理Bitmap内存 - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接   Managing Bitmap Memory 管理Bitmap内存 In additi ...

  9. (转载博文)MFC 窗口句柄获取

    句柄获取方法(获取该窗口的句柄后,即可向该窗口类类发送消息.处理程序):0.获取所在类窗口的句柄: this->m_hwnd 1.主窗口的句柄: 无论在主窗口类内,还是子窗口类内,获取主窗口句柄 ...

  10. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...