單例模式用於限制進程中只有一個某個類的對象,本例的Singleton是一個線程實例,在每一個時鐘到達時檢測是否到達某個時刻(本例的時刻存於Ini文件中),如果到達則產生一個線程,但是如果在這個線程完成其任務前又到達一個時鐘,則有可能會產生多個線程執行任務,以致出現混亂,所以考慮使用Singleton模式解決這個問題(當然還有其他解決方案,但本例使用的是Singleton)。
核心代碼如下:
//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.