單例模式用于限制進(jìn)程中只有一個(gè)某個(gè)類的對(duì)象,本例的Singleton是一個(gè)線程實(shí)例,在每一個(gè)時(shí)鐘到達(dá)時(shí)檢測是否到達(dá)某個(gè)時(shí)刻(本例的時(shí)刻存于Ini文件中),如果到達(dá)則產(chǎn)生一個(gè)線程,但是如果在這個(gè)線程完成其任務(wù)前又到達(dá)一個(gè)時(shí)鐘,則有可能會(huì)產(chǎn)生多個(gè)線程執(zhí)行任務(wù),以致出現(xiàn)混亂,所以考慮使用Singleton模式解決這個(gè)問題(當(dāng)然還有其他解決方案,但本例使用的是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','下發(fā)時(shí)間',theexename+'.ini'),4) then begin //創(chuàng)建發(fā)送線程 Global_Instance:=TSendThread.getInstance; ////////////// end; except on e: Exception do begin mystringlist:=TStringList.Create; if FileExists(ExtractFilePath(Paramstr(0))+'Err.txt') then mystringlist.LoadFromFile(ExtractFilePath(Paramstr(0))+'Err.txt'); mystringlist.Add('('+DateTimeToStr(Now)+')[創(chuàng)建線程出錯(cuò):]'+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; procedure joke;
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.joke; begin end; procedure TSendThread.Execute; var theuser:TUserInfo; tmpSql:string; begin //執(zhí)行任務(wù) //處理定時(shí)下發(fā) '+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; //任務(wù)完成 end; end.