国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Delphi > 正文

Delphi實(shí)現(xiàn)Singleton模式

2019-11-18 18:05:18
字體:
供稿:網(wǎng)友
單例模式用于限制進(jìn)程中只有一個(gè)某個(gè)類的對象,本例的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.

上一篇:Delphi編程實(shí)現(xiàn)Ping操作

下一篇:用Delphi2005和DUnit搭建敏捷開發(fā)平臺(tái)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門圖片

新聞熱點(diǎn)

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 辉县市| 巴里| 南通市| 岢岚县| 凤山市| 新乡县| 霍城县| 随州市| 贵南县| 遂昌县| 三门峡市| 丘北县| 潼关县| 绿春县| 祁门县| 泰顺县| 鲜城| 天峨县| 卢湾区| 张家口市| 西城区| 林周县| 汾西县| 行唐县| 黄梅县| 平塘县| 南和县| 呼玛县| 临朐县| 泰安市| 福鼎市| 新余市| 赤峰市| 色达县| 固安县| 尼玛县| 呼图壁县| 辽阳市| 新宾| 宁安市| 浠水县|