Singleton mode is used to restrict only one object of a certain class in the process. The Singleton in this example is a thread instance that detects whether a certain time has arrived when each clock arrives (the time of this example is stored in the Ini file), if Arrivals generate a thread, but if another clock is reached before this thread completes its task, it may generate multiple threads to execute the task, resulting in confusion, so consider using Singleton mode to solve this problem (of course there are other solutions. , but this example uses Singleton).
The core code is as follows:
//Timer unit
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','issuance time',theexename+'.ini'),4) then
Begin
//Create a sending thread
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)+')[Error creating thread:]'+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;
//Thread Unit
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
//Execute tasks
//Processing timely issuance '+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;
//The task is completed
end;
end.