O modo Singleton é usado para restringir apenas um objeto de uma determinada classe no processo. ), se as chegadas gerarem um tópico, mas se outro relógio for alcançado antes que esse tópico conclua sua tarefa, poderá gerar vários threads para executar a tarefa, resultando em confusão, então considere usar o modo singleton para resolver esse problema (é claro que existem outros soluções.
O código principal é o seguinte:
// Unidade de temporizador
Procedimento tService1.timer_maintimer (remetente: tabjas);
var
MyStringList: TStringList;
SearchRec: TSearchRec;
NowTime: String;
Começar
tentar
DATETIMETOSTRING (NowTime, 'hh: nn', agora);
Se LeftStr (NowTime, 4) = LeftStr (getmsg ('Game', 'Time de emissão', TheExename+'. Ini'), 4) então
Começar
// Crie um tópico de envio
Global_instance: = tsendThread.getInstance;
////////////////////////////////////////Uver
fim;
exceto
em e: exceção do
Começar
MyStringList: = tStringList.create;
Se fileExists (ExtractFilePath (paramstr (0))+'err.txt') então
myStringList.LoadFromFile (ExtractFilePath (paramstr (0))+'err.txt');
myStringList.add ('('+datetimetostr (agora)+') [erro de criação de thread:]'+e.message);
myStringList.Savetofile (ExtractFilePath (paramstr (0))+'err.txt');
myStringList.Free;
Se Findfirst (ExtractFilePath (paramstr (0))+'err.txt', faanyfile, Searchrec) = 0 então
Começar
Se Searchrec.size> 5000000, então
Começar
Renamefile (ExtractFilePath (paramstr (0))+'err.txt', Ansireplacest (ExtractFilePath (paramstr (0))+'err.txt', 'txt', formatDateTime ('yyyyy-mm-dd hh-ss ', agora)+'. txt '));
fim;
fim;
fim;
fim;
fim;
// unidade de thread
unidade unit_send;
interface
usos
Sysutils, classes, Strutils, Main;
tipo
TsendThread = classe (tThread)
público
Construtor Create (CreateSuspended: boolean);
destruidor destruir;
função de classe getInstance: tsendThread;
piada de procedimento;
protegido
procedimento executado;
fim;
var
Global_instance: TsendThread;
Implementação
usa db;
função de classe tsendThread.getInstance: tsendThread;
Começar
Se global_instance = nil então
Começar
Global_instance: = tsendThread.create (false);
fim;
Resultado: = global_instance;
fim;
construtor tsendThread.create (CreateSuspended: boolean);
Começar
Se global_instance = nil então
Começar
Criar herdado (CreateSuspended);
FreeOnTerMinate: = true;
fim
outro
levantar excepção.Createfmt ('Não pode criar mais de uma instância TsendThread!', [SySerRormessage (0)]);
fim;
destruidor tsendthread.destroy;
Começar
Destrução herdada;
fim;
procedimento tsendThread.joke;
Começar
fim;
procedimento tsendThread.execute;
var
THEURER: TUSERINFO;
tmpsql: string;
Começar
// executa tarefas
// Processando emissão oportuna '+gameinfo.mainUsertable+'
tmpsql: = 'selecione * de'+mainUsertable+'where Destroy = 0 Ordem por id';
Service1.adoQuery_send.Connection: = Conn_server;
Sqlquery (Service1.adoQuery_send, tmpsql);
enquanto (não service1.adoquery_send.eof) e (não terminado)
Começar
theUser.seqid: = '0';
theUser.uid: = '';
theUser.spc: = getmsg ('parâmetro', 'spcode', theExename+'. ini');
theUser.Recordid: = '0';
theUser.mob: = Service1.adoquery_send.fieldbyname ('celular'). Asstring;
AutoJoke (TheUser);
Service1.adoQuery_send.Next;
fim;
Sono (600001);
Global_instance: = nil;
Rescindir;
// A tarefa é concluída
fim;
fim.