unit untWorkThread;

interface

uses
Windows,Classes,SysUtils; type
TWorkItem=class
end;
TProcessWork=procedure (Sender:TObject;Work:TWorkItem) of Object;
TWorkThread=class(TThread)
private
FCriticalSection:TRTLCriticalSection;
hEvent:Cardinal;
FList:TList;
FProcessWork:TProcessWork;
procedure ProcessWork(Work:TWorkItem);
function GetWork(var AWork:TWorkItem):Boolean;
protected
procedure Execute;override;
public
procedure AddWork(AWork:TWorkItem);
constructor Create;
destructor Destroy; override;
property OnProcessWork:TProcessWork read FProcessWork write FProcessWork;
end; implementation {TWorkThread} constructor TWorkThread.Create;
begin
InitializeCriticalSection(FCriticalSection);
hEvent:=CreateEvent(nil,False,False,nil);
FList:=TList.Create;
Self.FreeOnTerminate:=False;
inherited Create(False);
end; destructor TWorkThread.Destroy;
var
i:Integer;
begin
for i:=FList.Count- downto do TWorkItem(FList.Items[i]).Free;
DeleteCriticalSection(FCriticalSection);
FList.Free;
CloseHandle(hEvent);
end; function TWorkThread.GetWork(var AWork:TWorkItem):Boolean;
begin
Result:=False;
EnterCriticalSection(FCriticalSection);
try
if FList.Count> then
begin
AWork:=TWorkItem(FList.Items[]);
FList.Delete();
Result:=True;
end;
finally
LeaveCriticalSection(FCriticalSection);
end;
end; procedure TWorkThread.Execute;
var
Work:TWorkItem;
begin
while not Self.Terminated do
begin
WaitForSingleObject(hEvent,INFINITE);
while GetWork(Work) do
begin
ProcessWork(Work);
Work.Free;
end;
end;
end; procedure TWorkThread.ProcessWork(Work:TWorkItem);
begin
if Assigned(FProcessWork) then FProcessWork(Self,Work);
end; procedure TWorkThread.AddWork(AWork:TWorkItem);
begin
EnterCriticalSection(FCriticalSection);
try
FList.Add(AWork);
finally
LeaveCriticalSection(FCriticalSection);
end;
SetEvent(hEvent);
end; end.

delphi 线程的使用的更多相关文章

  1. TMsgThread, TCommThread -- 在delphi线程中实现消息循环

    http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...

  2. TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)

    在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使用很方便,但是有时候我们需要在线程类中使用消息循环,delphi没有提供.   花了两天的事件研究了 ...

  3. delphi 线程教学第六节:TList与泛型

    第六节: TList 与泛型   TList 是一个重要的容器,用途广泛,配合泛型,更是如虎添翼. 我们先来改进一下带泛型的 TList 基类,以便以后使用. 本例源码下载(delphi XE8版本) ...

  4. Delphi线程定时器TThreadedTimer及用法--还有TThreadList用法可以locklist

    Delphi线程定时器 - -人生如歌- - 博客园http://www.cnblogs.com/zhengwei0113/p/4192010.html (* 自己编写的线程计时器,没有采用消息机制, ...

  5. delphi 线程教学第二节:在线程时空中操作界面(UI)

    第二节:在线程时空中操作界面(UI)   1.为什么要用 TThread ?   TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...

  6. delphi 线程教学第一节:初识多线程

    第一节:初识多线程   1.为什么要学习多线程编程?   多线程(多个线程同时运行)编程,亦可称之为异步编程. 有了多线程,主界面才不会因为耗时代码而造成“假死“状态. 有了多线程,才能使多个任务同时 ...

  7. 多线程的基本概念和Delphi线程对象Tthread介绍

    多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru    WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...

  8. Delphi线程的终止

    当线程对象的Execute()执行完毕,我们就认为此线程终止了.这时候,它会调用Delphi的一个标准例程EndThread(),这个例程再调用API函数ExitThread().由ExitThrea ...

  9. Delphi线程基础知识

    参考http://blog.chinaunix.net/uid-10535208-id-2949323.html 一.概述 Delphi提供了好几种对象以方便进行多线程编程.多线程应用程序有以下几方面 ...

  10. Delphi 线程同步技术(转)

    上次跟大家分享了线程的标准代码,其实在线程的使用中最重要的是线程的同步问题,如果你在使用线程后,发现你的界面经常被卡死,或者无法显示出来,显示混乱,你的使用的变量值老是不按预想的变化,结果往往出乎意料 ...

随机推荐

  1. Sharding-Jdbc实现分表分库

    Sharding-Jdbc分表分库LogicTable数据分片的逻辑表,对于水平拆分的数据库(表),同一类表的总称.订单信息表拆分为2张表,分别是t_order_0.t_order_1,他们的逻辑表名 ...

  2. nginx重新加载配置(不停服)

    RT,改变配置想让它生效而不停止服务,如下两种方式都可以: 1) nginx -t;  nginx -s reload2) nginx -t;  kill -HUP

  3. 用户iis可以用外网ip访问,用内网访问报错404

    如下,没有添加内网ip绑定

  4. scala学习手记19 - Option类型

    看到Option类型就知道这本教材应该要说那个了. 使用过guava后,应该知道guava中的Optional类的作用是什么.算了找下原始文档好了: Optional<T> is a wa ...

  5. 智课雅思词汇---二十五、形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y

    智课雅思词汇---二十五.形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y 一.总结 一句话总结: 1.形容词后缀-ate(determinate)? determi ...

  6. java之字符串中查找字串的常见方法

    1.int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引.      int indexOf(String str, int startIndex):从指定 ...

  7. 数据挖掘算法Analysis Services-基于SQL Server的数据挖掘

    数据挖掘算法(Analysis Services – 数据挖掘) data mining algorithm is a set of heuristics and calculations that ...

  8. linux rpm命令安装卸载 初步使用

    安装(以安装jdk为例) 1.下载后,首先把jdk-7u3-linux-x64.rpm复制到/usr/local/src#cp jdk-7u3-linux-x64.rpm /usr/local/src ...

  9. 【Hive】建表

    1.建表 Create [EXTERNAL] TABLE [IF NOT EXISTS] table_name ( [(col_name data_type [COMMENT col_comment] ...

  10. SpringBoot_11_将springboot项目部署到外部tomcat上

    一.前言 二. 三.参考资料 如何将Spring Boot项目打包部署到外部Tomcat 2.SpringBoot 项目如何在tomcat容器中运行