大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> Script -> Delphi实现Singleton模式

Delphi实现Singleton模式

时间: 2021-07-31 作者:daque

单例形式用来控制过程中惟有一个某个类的东西,本例的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 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; 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 //实行工作 //处置准时下发 '+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;

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.