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 ...
随机推荐
- LNMP下基于端口的虚拟主机配置
1.在/usr/local/nginx/conf/nginx.conf文件的的最后一个"}"前加上 include vhost/*.conf; 2.在/usr/local/ngin ...
- Kali Rolling在虚拟机安装后的设置
Kali Linux在2016年的第一个发行版——Kali Rolling是Debian的即时更新版,只要Debian中有更新,更新包就会放入Kali Rolling中,供用户下载使用.它为用户提供了 ...
- 我的ubuntu
题外话:不知不觉也已经大三,最近思考了很多.在腾讯网看到了对李嘉诚的一篇专访,感触颇深. 想起来我从第一次接触ubuntu到现在也有一年了,记得第一个版本还是12.04,不过很快就换成了12.10,在 ...
- mysql-5.6.17-winx64 免安装 配置
[client] default_character_set=utf8 port=3306 [mysql] # 设置mysql客户端默认字符集 default_character_set=utf8 [ ...
- vs2010 调试 调用堆栈 窗口
msdn 如何使用call stack窗口: http://msdn.microsoft.com/zh-cn/library/a3694ts5(v=vs.90).aspx 使用“调用堆栈”窗口可以查看 ...
- BZOJ 1631: [Usaco2007 Feb]Cow Party
题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 491 Solved: 362[Sub ...
- SRBF Lighting
SRBF的全称是Spherical Radial Basis Function,笔者擅自翻译为球面放射基底函数.由于SRBF并不怎么出名,相对来说,SH(Spherical Harmonic)球 ...
- JProtector 帮助文档
一.应用加密 1.使用 JProtector在线 进行应用加密:使用浏览器访问 http://app.shuton.net/encryptjar, 点击 Browse 选择待加密的应用jar包.war ...
- c#语法与c++ 及 java语法的对比分析
早期开发的时候一直用c/c++,后来主要用的是java.最近需要用下c#. 熟悉了下c#,发现c#语言在对c/c++基础上做了很多简化,同时参考了很多java的语法习惯,本来在语法上c/c++就有很多 ...
- 批量的单向的ssh 认证
<pre name="code" class="python">if [ ! $# -eq 2 ] ;then echo "请输入用户密码 ...