TTimerThread和TThreadedTimer(都是通过WaitForSingleObject和CreateEvent来实现的)
////////////////////////////////////////////////////
// //
// ThreadedTimer 1.24 //
// //
// Copyright (C) 1996, 2000 Carlos Barbosa //
// email: delphi@carlosb.com //
// Home Page: http://www.carlosb.com //
// //
// Portions (C) 2000, Andrew N. Driazgov //
// email: andrey@asp.tstu.ru //
// //
// Last updated: November 24, 2000 //
// //
//////////////////////////////////////////////////// unit ThdTimer; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; const
DEFAULT_INTERVAL = ; type
TThreadedTimer = class; TTimerThread = class(TThread)
private
FOwner: TThreadedTimer;
FInterval: Cardinal;
FStop: THandle;
protected
procedure Execute; override;
end; TThreadedTimer = class(TComponent)
private
FOnTimer: TNotifyEvent;
FTimerThread: TTimerThread;
FEnabled,
FAllowZero: Boolean; procedure DoTimer; procedure SetEnabled(Value: Boolean);
function GetInterval: Cardinal;
procedure SetInterval(Value: Cardinal);
function GetThreadPriority: TThreadPriority;
procedure SetThreadPriority(Value: TThreadPriority);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override; published
property AllowZero: Boolean read FAllowZero write FAllowZero default False;
property Enabled: Boolean read FEnabled write SetEnabled default False;
property Interval: Cardinal read GetInterval write SetInterval default DEFAULT_INTERVAL;
property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;
property ThreadPriority: TThreadPriority read GetThreadPriority write SetThreadPriority default tpNormal;
end; procedure Register; implementation { TTimerThread } procedure TTimerThread.Execute;
begin
repeat
if WaitForSingleObject(FStop, FInterval) = WAIT_TIMEOUT then
Synchronize(FOwner.DoTimer);
until Terminated;
end; { TThreadedTimer } constructor TThreadedTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FTimerThread := TTimerThread.Create(True);
with FTimerThread do
begin
FOwner := Self;
FInterval := DEFAULT_INTERVAL;
Priority := tpNormal; // Event is completely manipulated by TThreadedTimer object
FStop := CreateEvent(nil, False, False, nil);
end;
end; destructor TThreadedTimer.Destroy;
begin
with FTimerThread do
begin
FEnabled := False; Terminate; // When this method is called we must be confident that the event handle was not closed
SetEvent(FStop);
if Suspended then
Resume;
WaitFor;
CloseHandle(FStop); // Close event handle in the primary thread
Free;
end;
inherited Destroy;
end; procedure TThreadedTimer.DoTimer;
begin // We have to check FEnabled in the primary thread
// Otherwise we get AV when the program is closed
if FEnabled and Assigned(FOnTimer) and not (csDestroying in ComponentState) then
try
FOnTimer(Self);
except
end;
end; procedure TThreadedTimer.SetEnabled(Value: Boolean);
begin
if Value <> FEnabled then
begin
FEnabled := Value;
if FEnabled then
begin
if (FTimerThread.FInterval > ) or
((FTimerThread.FInterval = ) and FAllowZero) then
begin
SetEvent(FTimerThread.FStop);
FTimerThread.Resume;
end;
end
else
FTimerThread.Suspend;
end;
end; function TThreadedTimer.GetInterval: Cardinal;
begin
Result := FTimerThread.FInterval;
end; procedure TThreadedTimer.SetInterval(Value: Cardinal);
var
PrevEnabled: Boolean;
begin
if Value <> FTimerThread.FInterval then
begin
PrevEnabled := FEnabled;
Enabled := False;
FTimerThread.FInterval := Value;
Enabled := PrevEnabled;
end;
end; function TThreadedTimer.GetThreadPriority: TThreadPriority;
begin
Result := FTimerThread.Priority;
end; procedure TThreadedTimer.SetThreadPriority(Value: TThreadPriority);
begin
FTimerThread.Priority := Value;
end; procedure Register;
begin
RegisterComponents('System', [TThreadedTimer]);
end; end.
TTimerThread和TThreadedTimer(都是通过WaitForSingleObject和CreateEvent来实现的)的更多相关文章
- 事件EVENT与waitforsingleobject的使用以及Mutex与Event的区别
Mutex与Event控制互斥事件的使用详解 最近写一程序,误用了Mutex的功能,错把Mutex当Event用了. [Mutex] 使用Mutex的主要函数:CreateMutex.ReleaseM ...
- [转]Linux 的多线程编程的高效开发经验
Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...
- Linux 的多线程编程的高效开发经验(转)
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- Linux 的多线程编程的高效开发经验
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- 线程、线程句柄、线程ID
什么是句柄:句柄是一种指向指针的指针.我们知道,所谓指针是一种内存地址.应用程序启动后,组成这个程序的各对象是住留在内存的.如果简单地理解,似乎我们只要获知这个内存的首地址,那么就可以随时用这个地址 ...
- C++ MFC常用函数(转)
WinExec() ExitWindowsEx() GlobalMemoryStatus() GetSystemInfo() GetSystemDirectory() GetWindowsDirect ...
- 线程模型、pthread 系列函数 和 简单多线程服务器端程序
一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属于1:1模型. (一).N:1用户线程模型 “线程实现”建立在“进程控制”机制之上,由用 ...
- MFC常用函数总结
1.MFC编辑框.静态文本框相关的常用函数 <1>GetDlgItemText(ID ,str) 作用:从对话框中获取文本 第一个参数为要获取的编辑框(或者静态文本框.单选按钮等可以显示内 ...
- C/C++ 信号量 CreateSemaphore 用法
HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD LONG lInitialCount, // in ...
随机推荐
- C# Programming Study #2
readonly (C# Reference) readonly 关键字是可以在字段上使用的修饰符. 当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者 ...
- OC基础教程 C语言中的格式占位符:
C语言中的格式占位符: %a,%A 读入一个浮点值(仅C99有效) %c 读入一个字符 %d 读入十进制整数 %i 读入十进制,八进制,十六进制整数 %o 读入八进制整数 %x,%X 读入十六进制整数 ...
- 深入浅出—JAVA(7)
7.继承与多态 遵守合约:覆盖的规则 方法的重载
- 转: requirejs中文api (详细)
RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代码的模块化.它鼓励在使用脚本时以modul ...
- 链接分析算法之:主题敏感PageRank
链接分析算法之:主题敏感PageRank 前面的讨论提到.PageRank忽略了主题相关性,导致结果的相关性和主题性降低,对于不同的用户,甚至有很大的差别.例如,当搜索“苹果”时,一个数码爱好 ...
- poj 1664 put apples(dfs)
题目链接:http://poj.org/problem?id=1664 思路分析:数据较小,考虑深度优先搜索搜索解空间. 代码如下: #include <iostream> using n ...
- UVA 10652 Board Wrapping(凸包)
The small sawmill in Mission, British Columbia, hasdeveloped a brand new way of packaging boards for ...
- Dijkstra 模板 最短路
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents ------------------------------------------ ...
- java学习——入门扫盲篇
概要 近期这几天開始进入java的学习,接触到了好多不是非常了解的概念,像JDK.JRE.JVM.GC等等这些,放到这里来进行下扫盲. java java是一种面向对象程序设计语言和java平台的总称 ...
- 【OpenMesh】使用网格的属性和特征
例子主要展示如何改变位置,法向量,颜色和纹理的数据类型. 在之前的指南中我们学习使用标准属性,通过调用适合的请求方法.不像自定义属性,用户通过传递数据类型到句柄来指定数据类型(比如,MyMesh::F ...