用Delphi实现Singleton模式
2008-02-23 07:18:24来源:互联网 阅读 ()
核心代码如下:
//timer单元
procedure TService1.Timer_mainTimer(Sender: TObject);
var
mystringlist:TStringList;
SearchRec: TSearchRec;
nowtime :string;
begin
try
DateTimeToString(nowtime,'hh:nn',now);
if LeftStr(nowtime,4)=LeftStr(GetMSG('GAME','下发时间',theexename '.ini'),4) then
begin
//创建发送线程
Global_Instance:=TSendThread.getInstance;
//////////////
end;
except
on e: Exception do
begin //捕获错误存入txt文件
mystringlist:=TStringList.Create;
if FileExists(ExtractFilePath(Paramstr(0)) 'Err.txt') then
mystringlist.LoadFromFile(ExtractFilePath(Paramstr(0)) 'Err.txt');
mystringlist.Add('(' DateTimeToStr(Now) ')[创建线程出错:]' E.Message);
mystringlist.SaveToFile(ExtractFilePath(Paramstr(0)) 'Err.txt');
mystringlist.Free;
if FindFirst(ExtractFilePath(Paramstr(0)) 'Err.txt', faAnyFile, SearchRec)=0 then
begin
if SearchRec.Size>5000000 then
begin
RenameFile(ExtractFilePath(Paramstr(0)) 'Err.txt',ansireplacestr(ExtractFilePath(Paramstr(0))
'Err.txt','.txt',FormatDateTime('yyyy-MM-dd hh-mm-ss',now) '.txt'));
end;
end;
end;
end;
end;
//线程单元
unit Unit_Send ;
interface
uses
SysUtils, Classes,StrUtils,main;
type
TSendThread = class(TThread)
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy; override;
class function getInstance:TSendThread;
protected
procedure Execute; override;
end;
var
Global_Instance:TSendThread;
implementation
uses DB;
class function TSendThread.getInstance:TSendThread;
begin
if Global_Instance=nil then
begin
Global_Instance:=TSendThread.Create(false);
end;
Result:=Global_Instance;
end;
constructor TSendThread.Create(CreateSuspended: Boolean);
begin
if Global_Instance=nil then
begin
inherited Create(CreateSuspended);
FreeOnTerminate:=true ;
end
else //如果有人不小心多次创建对象则产生一个异常
raise Exception.CreateFmt('Can not create more than one TSendThread instance!',[SysErrorMessage(0)]);
end;
destructor TSendThread.Destroy;
begin
inherited Destroy;
end;
procedure TSendThread.Execute;
var
theuser:TUserInfo;
tmpSql:string;
begin
//执行任务
//处理定时下发 ' GameInfo.mainusertable '
tmpSql:='select * from ' mainusertable ' where destroy=0 order by id';
Service1.ADOQuery_send.Connection:=conn_Server;
SQLQuery(Service1.ADOQuery_send,tmpSql);
while (not Service1.ADOQuery_send.Eof) and (not Terminated) do
begin
theuser.SeqID:='0';
theuser.UID:='';
theuser.Spc:=GetMSG('PARAMETER','Spcode',theexename '.ini');
theuser.RecordID:='0';
theuser.Mob:=Service1.ADOQuery_send.FieldByname('mobile').AsString;
AutoJoke(theuser);
Service1.ADOQuery_send.Next;
end;
Sleep(600001);
Global_Instance:=nil;
Terminate;
//任务完成
end;
end.
上一篇: Delphi2005和DUnit搭建敏捷开发平台
下一篇: 用Delphi来DIY一个软件"变脸"功能
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash