////////////////////////////////////////////////////
// //
// 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来实现的)的更多相关文章

  1. 事件EVENT与waitforsingleobject的使用以及Mutex与Event的区别

    Mutex与Event控制互斥事件的使用详解 最近写一程序,误用了Mutex的功能,错把Mutex当Event用了. [Mutex] 使用Mutex的主要函数:CreateMutex.ReleaseM ...

  2. [转]Linux 的多线程编程的高效开发经验

    Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...

  3. Linux 的多线程编程的高效开发经验(转)

    http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...

  4. Linux 的多线程编程的高效开发经验

    http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...

  5. 线程、线程句柄、线程ID

     什么是句柄:句柄是一种指向指针的指针.我们知道,所谓指针是一种内存地址.应用程序启动后,组成这个程序的各对象是住留在内存的.如果简单地理解,似乎我们只要获知这个内存的首地址,那么就可以随时用这个地址 ...

  6. C++ MFC常用函数(转)

    WinExec() ExitWindowsEx() GlobalMemoryStatus() GetSystemInfo() GetSystemDirectory() GetWindowsDirect ...

  7. 线程模型、pthread 系列函数 和 简单多线程服务器端程序

    一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属于1:1模型. (一).N:1用户线程模型 “线程实现”建立在“进程控制”机制之上,由用 ...

  8. MFC常用函数总结

    1.MFC编辑框.静态文本框相关的常用函数 <1>GetDlgItemText(ID ,str) 作用:从对话框中获取文本 第一个参数为要获取的编辑框(或者静态文本框.单选按钮等可以显示内 ...

  9. C/C++ 信号量 CreateSemaphore 用法

    HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD LONG lInitialCount, // in ...

随机推荐

  1. FPGA知识大梳理(二)verilogHDL语法入门(1)

    此文是写给0基础学习者,也是对自己知识点总结水平的考验. 对于有C基础的人来说,学习verilog应该是轻而易举 —— 类比法学习. 第一步:格式. 对于C来说我们前面会写  ‘include“std ...

  2. 我在北京找工作(二):java实现算法<1> 冒泡排序+直接选择排序

    工作.工作.找工作.经过1个多星期的思想斗争还是决定了找JAVA方面的工作,因为好像能比PHP的工资高点.呵呵 :-)  (其实我这是笑脸,什么QQ输入法,模拟表情都没有,忒不人性化了.) 言归正传, ...

  3. 在优化SQL语句中使用虚拟索引

    定义:虚拟索引(virtual index) 是指没有创建对应的物理段的索引. 虚拟索引的目的:是在不损耗主机CPU,IO,磁盘空间去实际创建索引的情况下,来判断一个索引是否能够对SQL优化起到作用. ...

  4. Vmware ESX 5.0 安装与部署

    近期我公司部署了虚拟化,採购了两台Dell R710的server(CPU:64位双核,主频:2.4GHZ, 32G 内存.硬盘:2块300G做Riad 1.3块2T做Riad 5 .10块网卡),在 ...

  5. 记录一次SQL查询语句

    以前发现比较经典的句子,都是记录在电脑上,我今天想搬到博客上,在我看来,写博客真的是一件非常头疼的事,它是内心的一道坎,我必须得跨过它. CREATE TABLE t_jeff ( id int NO ...

  6. Android广播——短信拦截

    MainActivity.java package com.example.broadcasttest; import android.content.Intent; import android.c ...

  7. Miller_Rabin、 Pollard_rho Template

    Multiply and pow Function: //计算 (a*b)%c. a,b都是ll的数,直接相乘可能溢出的 // a,b,c <2^63 ll mult_modq(ll a,ll ...

  8. Python 2.7 学习笔记 字典(map)的使用

    python中的字典,就是通常说的map,即 key/value集合的数据结构. 本文来介绍下在python下如何使用字典. 对于map这种数据结构能干什么,我们就不说了,这是一个常见的数据结构,我们 ...

  9. Maven创建项目: Failed to execute goal org.apache.maven.plugin( mvn archetype:create)

    一.概述: 在使用mvn 命令mvn archetype:create -DgroupId=com.chuanliu.c11 -DartifactId=c11searcher在控制创建maven项目和 ...

  10. Ruby学习-第二章

    第二章 类继承,属性,类变量 1.如何声明一个子类 class Treasure < Thing 这样Thing类中的属性name,description都被Treasure继承 2.以下三种方 ...