博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi线程类(四)
阅读量:5891 次
发布时间:2019-06-19

本文共 3658 字,大约阅读时间需要 12 分钟。

View Code
{
*******************************************************}{
}{
Delphi Thread Sample 4 }{
Creation Date 2012.12.21 }{
Created By: ming }{
}{
*******************************************************}unit unitWorkThread;interfaceuses Classes,Windows, Messages, SysUtils, Graphics, StdCtrls;type TWorkThread = class(TThread) private {
Private declarations } FEvent: HWND; FMsg: string; FMemo: TMemo; FInterval,FTickTimes: Cardinal; procedure doSyncProc1; procedure syncOutputMsg; procedure addLog(const msg: string); overload; procedure addLog(const fmtStr:string; const params: array of const); overload; procedure _sleep(millisecond:Cardinal); protected procedure Execute; override; public constructor Create(Suspend: boolean); overload; constructor Create(Suspend: boolean; mmoOutput: TMemo); overload; destructor Destroy; override; private FThreadPause,FThreadStop: Boolean; procedure doSomething; public function ThreadStart: Boolean; function ThreadPause: Boolean; function ThreadStop: Boolean; procedure ThreadTerminate; public property Interval:Cardinal read FInterval write FInterval; end;var WorkThread: TWorkThread;implementation{
TWorkThread }constructor TWorkThread.Create(Suspend: boolean);begin inherited Create(Suspend); FEvent := CreateEvent(nil,True,False,nil); FreeOnTerminate := True; FInterval := 1000;end;procedure TWorkThread.addLog(const msg: string);begin FMsg := msg; Synchronize(syncOutputMsg);end;procedure TWorkThread.addLog(const fmtStr: string; const params: array of const);begin FMsg := Format(fmtStr,params); Synchronize(syncOutputMsg);end;constructor TWorkThread.Create(Suspend: boolean; mmoOutput: TMemo);begin inherited Create(Suspend); FEvent := CreateEvent(nil,True,False,nil); FreeOnTerminate := True; FInterval := 1000; FMemo := mmoOutput;end;destructor TWorkThread.Destroy;begin CloseHandle(FEvent); inherited;end;procedure TWorkThread.doSomething;begin addLog(FormatDateTime('c',now));end;procedure TWorkThread.doSyncProc1;beginend;procedure TWorkThread.syncOutputMsg;var dt: string;begin dt := FormatDateTime('hh:nn:ss',now); FMsg := Format('[%s] - ',[dt]) + FMsg; if Assigned(FMemo) then FMemo.Lines.Add(FMsg);end;procedure TWorkThread.Execute;begin inherited; while not Terminated do begin if WaitForSingleObject(FEvent,100)=WAIT_OBJECT_0 then begin Break; end; if (GetTickCount - FTickTimes) >= FInterval then try if not FThreadStop then begin doSomething; FTickTimes := GetTickCount; end; except on e:Exception do addLog(e.Message); end; if FThreadStop then Suspend; end;end;function TWorkThread.ThreadStart: Boolean;begin FThreadStop := False; if Suspended then Resume;end;function TWorkThread.ThreadPause: Boolean;begin FThreadPause := True; if not Suspended then Suspend;end;function TWorkThread.ThreadStop: Boolean;begin FThreadPause := False; FThreadStop := True; if Suspended then Resume;end;procedure TWorkThread.ThreadTerminate;begin FThreadStop := False; if FEvent>0 then begin SetEvent(FEvent); if Suspended then Resume; end;end;procedure TWorkThread._sleep(millisecond: Cardinal);begin //WaitForSingleObject(Self.Handle,millisecond); WaitForSingleObject(FEvent,millisecond);end;end.

 

转载于:https://www.cnblogs.com/Jekhn/archive/2012/12/23/2829959.html

你可能感兴趣的文章
GoogleMapAPIV3.8.6离线包下载
查看>>
SILK 的 Tilt的意思
查看>>
IPC通信:Posix共享内存2
查看>>
GB2312转成UTF-8
查看>>
C#打开chm定位到特定页面
查看>>
[CareerCup][Google Interview] 寻找动态的中位数
查看>>
javascript操作iframe的那些事
查看>>
servlet相关 jar包位置 BAE上部署web应用
查看>>
路徑 z
查看>>
cpu分析简介
查看>>
1.备忘录模式
查看>>
Html学习笔记3
查看>>
phpexcel导出超过26列解决方案
查看>>
【SSH网上商城项目实战04】EasyUI菜单的实现
查看>>
阿里云云服务器硬盘分区及挂载
查看>>
数据库:django ORM如何处理N+1查询
查看>>
python 基础干货 02
查看>>
[React Router v4] Redirect to Another Page
查看>>
void 0 或者 undefined
查看>>
OCIEnvCreate 失败,返回代码为 -1的解决方法
查看>>