在线程中使用定时器的例子!
2011-12-09 12:03
162 查看
在CSDN中找到的例子,自己测试了一下。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TCheckExternalButtonThread = class(TThread) private { Private declarations } FTimer:TTimer; procedure TimerProc(Sender: TObject); procedure KillIt; protected procedure Execute;override; public ThreadNo:THandle; constructor Create(); end; TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; ss:TCheckExternalButtonThread ; implementation {$R *.dfm} constructor TCheckExternalButtonThread.Create(); begin inherited Create(False); end; procedure TCheckExternalButtonThread.KillIt; begin PostThreadMessage(ThreadNo,WM_QUIT,0,0); end; procedure TCheckExternalButtonThread.TimerProc(Sender: TObject); begin Form1.Memo1.Lines.Add('asdasf'); end; procedure TCheckExternalButtonThread.Execute; var MsgRec:TMsg; begin FTimer:=TTimer.Create(nil); try FTimer.Enabled:=True;//需要用的时候Enabled设置为True即可。 FTimer.OnTimer:=TimerProc; FTimer.Interval:=100; while GetMessage(MsgRec, 0, 0, 0) do begin TranslateMessage(MsgRec); DispatchMessage(MsgRec) end; Terminate; finally FTimer.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin ss:=TCheckExternalButtonThread.Create; ss.ThreadNo:=ss.ThreadID; end; procedure TForm1.Button2Click(Sender: TObject); begin ss.KillIt; end; end.
相关文章推荐
- 类中使用线程例子5
- LINUX中使用PTHREAD_KILL函数测试线程是否存活的例子
- 定时器与多线程 SetTimer and Multi-Thread 每个线程独立使用一个定时器
- 非分离线程未使用join函数例子:
- java中使用线程实现Timer(定时器)原理和源码
- 下面的例子演示了100个线程同时向一个银行账户中存入1元钱,在没有使用同步机制和使用同步机制情况下的执行情况。
- linux中使用pthread_kill函数测试线程是否存活的例子
- 不良代码展示-Activity中使用线程的例子
- 一个使用线程计数器的例子
- 最简单的使用线程的例子
- Android使用handler实现简单的定时器,主线程每秒刷新UI
- QT中使用另外的线程运行定时器
- python串口编程,使用线程定时器
- QT之使用QReadWriteLocker互斥量同步线程小例子
- 使用pthread_cond_timedwait把一个线程作为一个定时器使用.
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- 线程互斥锁的问题(线程中使用定时器信号)
- Windows 游戏编程大师技巧第四章第6个例子---定时器的使用
- 一个关于looper和handler的好玩例子(子线程中使用handler)
- 使用定时器实现线程控制