delphi 线程的使用
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 线程的使用的更多相关文章
- TMsgThread, TCommThread -- 在delphi线程中实现消息循环
http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...
- TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)
在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使用很方便,但是有时候我们需要在线程类中使用消息循环,delphi没有提供. 花了两天的事件研究了 ...
- delphi 线程教学第六节:TList与泛型
第六节: TList 与泛型 TList 是一个重要的容器,用途广泛,配合泛型,更是如虎添翼. 我们先来改进一下带泛型的 TList 基类,以便以后使用. 本例源码下载(delphi XE8版本) ...
- Delphi线程定时器TThreadedTimer及用法--还有TThreadList用法可以locklist
Delphi线程定时器 - -人生如歌- - 博客园http://www.cnblogs.com/zhengwei0113/p/4192010.html (* 自己编写的线程计时器,没有采用消息机制, ...
- delphi 线程教学第二节:在线程时空中操作界面(UI)
第二节:在线程时空中操作界面(UI) 1.为什么要用 TThread ? TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...
- delphi 线程教学第一节:初识多线程
第一节:初识多线程 1.为什么要学习多线程编程? 多线程(多个线程同时运行)编程,亦可称之为异步编程. 有了多线程,主界面才不会因为耗时代码而造成“假死“状态. 有了多线程,才能使多个任务同时 ...
- 多线程的基本概念和Delphi线程对象Tthread介绍
多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...
- Delphi线程的终止
当线程对象的Execute()执行完毕,我们就认为此线程终止了.这时候,它会调用Delphi的一个标准例程EndThread(),这个例程再调用API函数ExitThread().由ExitThrea ...
- Delphi线程基础知识
参考http://blog.chinaunix.net/uid-10535208-id-2949323.html 一.概述 Delphi提供了好几种对象以方便进行多线程编程.多线程应用程序有以下几方面 ...
- Delphi 线程同步技术(转)
上次跟大家分享了线程的标准代码,其实在线程的使用中最重要的是线程的同步问题,如果你在使用线程后,发现你的界面经常被卡死,或者无法显示出来,显示混乱,你的使用的变量值老是不按预想的变化,结果往往出乎意料 ...
随机推荐
- LeetCode——Add Strings
LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...
- ThinkPHP关于模板的一些嵌套、IF判断使用
> ##### 前言,现在有一组数据(涉及到3个数据表,order订单表,order_process办理流程表,process_status流程描述表),根据当前订单,展示相应信息 1.办理流程 ...
- VLAN虚拟局域网技术(一)-计算机网络
本文主要知识来源于学校课程,部分知识来自于H3C公司教材,未经许可,禁止转载.如需转载,请联系作者并注明出处. 1. VLAN(Virtual LAN):我们称之为虚拟局域网,它的作用就是将物理上互 ...
- PHP 开发环境搭建
1. PHP (1) download PHP and extra the zip file to the folder “C:\tools\php” (2) add the path “;C:\to ...
- scala学习手记22 - 参数化类型的可变性
java 的代码中多少有些不是很严谨的内容,比如下面的这段代码: public class Trouble { public static void main(String[] args) { Int ...
- python urllib2库的简单总结
urllib2的简单介绍参考网址:http://www.voidspace.org.uk/python/articles/urllib2.shtml Fetching URLsThe simplest ...
- web自动化流程总结
一. 了解需求,什么是系统的核心业务 二. 编写测试用例:用例名称,前置条件,测试数据,测试步骤,期望结果 三. 自动化代码的初步构建:所有的元素定位.元素操作.测试用例都写在一个模块中 问题: 1. ...
- windows平台下安装Beautiful Soup
1.Beautiful Soup 4.3.2 下载 2.下载完成后解压,放在Python的安装目录下,假设放到C:/Python 3.运行cmd,切换到C:/Python/Beautiful Soup ...
- Mac安装Virtualenv & PyCharm使用Virtualenv
我们使用Python开发的时候,每个项目可能需要不同的Python版本,这时候我们就需要Virtualenv配置虚拟环境. Mac安装Virtualenv sudo pip install virtu ...
- 详述 SQL 中的 distinct 和 row_number() over() 的区别及用法
1 前言 在咱们编写 SQL 语句操作数据库中的数据的时候,有可能会遇到一些不太爽的问题,例如对于同一字段拥有相同名称的记录,我们只需要显示一条,但实际上数据库中可能含有多条拥有相同名称的记录,从而在 ...