싱글 톤 모드는 프로세스에서 특정 클래스의 하나의 객체 만 제한하는 데 사용됩니다. ), 도착이 스레드를 생성하지만이 스레드가 작업을 완료하기 전에 다른 시계에 도달하면 작업을 실행하기 위해 여러 스레드가 생성되어 혼란을 초래할 수 있으므로 싱글 톤 모드를 사용 하여이 문제를 해결하는 것을 고려하십시오 (물론 다른 사람이 있습니다. 솔루션. 그러나 이것은 싱글 톤을 사용합니다.
핵심 코드는 다음과 같습니다.
// 타이머 장치
절차 tservice1.Timer_MaintImer (sender : tobject);
var
mystringlist : tstringlist;
SearchRec : TsearchRec;
nowtime : 문자열;
시작하다
노력하다
DateTimetoString (nowtime, 'hh : nn', now);
leftstr (nowtime, 4) = leftstr (getmsg ( '게임', '발행 시간', exename+'. ini'), 4)
시작하다
// 보내는 스레드를 만듭니다
Global_Instance : = tsendthread.getInstance;
///////////////////////////////
끝;
제외하고
e : 예외적입니다
시작하다
mystringlist : = tstringlist.create;
pilexists (extractfilepath (paramstr (0))+'err.txt')
mystringList.loadFromFile (ExtractFilePath (paramstr (0))+'err.txt');
mystringList.add ( '('( '+dateTimeToStr (now)+') [스레드 생성 오류 :] '+e.message);
mystringList.savetofile (ExtractFilePath (paramstr (0))+'err.txt');
mystringlist.free;
FindFirst (ExtractFilePath (Paramstr (0))+'err.txt', faanyfile, searchRec) = 0 인 경우
시작하다
SearchRec.Size> 5000000 인 경우
시작하다
RenameFile (ExtractFilePath (Paramstr (0))+'err.txt', ansireplacest (extractFilePath (paramstr (0))+'err.txt', '. txt', formatdateTime ( 'yyyy-mm-dd hh-mm-ss ', 지금)+'. txt ');
끝;
끝;
끝;
끝;
끝;
// 스레드 장치
Unit_Send;
인터페이스
용도
sysutils, classes, strutils, main;
유형
tsendthread = class (tthread)
공공의
생성자 생성 (CreateSuspended : Boolean);
소멸자 파괴;
클래스 함수 getinstance : tsendthread;
절차 농담;
보호
절차 실행; 우정;
끝;
var
Global_instance : Tsendthread;
구현
DB를 사용합니다.
클래스 함수 tsendthread.getinstance : tsendthread;
시작하다
global_instance = nil이면
시작하다
Global_instance : = tsendthread.create (false);
끝;
결과 : = global_instance;
끝;
생성자 tsendthread.create (CreateSuspended : boolean);
시작하다
global_instance = nil이면
시작하다
상속 된 생성 (CreateSuspended);
FreeOnterminate : = 참;
끝
또 다른
exception.createfmt를 올리십시오 ( '하나 이상의 tsendthread 인스턴스를 만들 수 없습니다!', [syserRormessage (0)];
끝;
Destructor Tsendthread.destroy;
시작하다
상속 된 파괴;
끝;
절차 tsendthread.joke;
시작하다
끝;
절차 tsendthread.execute;
var
Theuser : Tuserinfo;
tmpsql : 문자열;
시작하다
// 작업을 실행합니다
// 적시 발행 처리 '+GameInfo.MainUsertable+'
tmpsql : = 'select * from'+mainUsertable+'여기서 destrove = 0 주문에 의해 주문';
Service1.adoQuery_Send.connection : = Conn_Server;
sqlquery (service1.adoquery_send, tmpsql);
while (service1.adoquery_send.eof)이 아닙니다 (종료되지 않음)
시작하다
theuser.seqid : = '0';
theuser.uid : = '';
theuser.spc : = getmsg ( '매개 변수', 'spcode', exename+'. ini');
theuser.recordid : = '0';
theuser.mob : = service1.adoquery_send.fieldByName ( 'mobile'). asstring;
오토 존 (Theuser);
Service1.adoquery_send.next;
끝;
수면 (600001);
Global_instance : = nil;
끝내다;
// 작업이 완료되었습니다
끝;
끝.