Le mode singleton est utilisé pour restreindre un seul objet d'une certaine classe dans le processus. ), si les arrivées génèrent un thread, mais si une autre horloge est atteinte avant que ce thread ne termine sa tâche, elle peut générer plusieurs threads pour exécuter la tâche, ce qui entraîne une confusion, alors envisagez d'utiliser le mode Singleton pour résoudre ce problème (bien sûr, il y en a d'autres Solutions.
Le code central est le suivant:
// Unité de minuterie
Procédure tService1.timer_mainmer (expéditeur: tobject);
var
MyStringList: TStringList;
Searchrec: tsearchrec;
maintenant temps: String;
Commencer
essayer
DateTimeToString (maintenant, 'hh: nn', maintenant);
Si leftstr (maintenant, 4) = leftstr (getmsg ('jeu', 'issu time', theexename + '. ini'), 4) alors
Commencer
// Créer un fil d'envoi
Global_instance: = tsendthread.getInstance;
///////////////////////////////////////
fin;
sauf
sur e: exception faire
Commencer
myStringList: = tStringList.Create;
Si des fichiers (ExtractFilePath (paramstr (0)) + 'err.txt') alors
MyStringList.LoadFromFile (ExtractFilePath (paramstr (0)) + 'err.txt');
myStringList.add ('(' + dateTiMetOstr (maintenant) + ') [Erreur Création de thread:]' + e.Message);
myStringList.savetofile (extractFilePath (paramstr (0)) + 'err.txt');
myStringList.free;
Si findFirst (ExtractFilePath (paramstr (0)) + 'err.txt', faanyfile, searchrec) = 0 alors
Commencer
Si searchrec.size> 5000000 alors
Commencer
RenameFile (ExtractFilePath (paramstr (0)) + 'err.txt', anSirePlacestr (ExtractFilePath (paramstr (0)) + 'err.txt', '. ', maintenant) +'. txt '));
fin;
fin;
fin;
fin;
fin;
// unité de filetage
UNIT UNIT_SEND;
interface
usages
Sysutils, classes, strutils, main;
taper
TSendThread = classe (tthread)
publique
Concluteur Create (Créer, Boolean);
Destructor détruire;
Fonction de classe GetInstance: tSendThread;
blague de procédure;
protégé
procédure d'exécution;
fin;
var
Global_instance: tSendThread;
Mise en œuvre
utilise db;
Fonction de classe tSendThread.getInstance: tSendThread;
Commencer
Si global_instance = nil alors
Commencer
Global_instance: = tsendthread.create (false);
fin;
Résultat: = global_instance;
fin;
Constructeur TSendThread.Create (CreateSpened: Boolean);
Commencer
Si global_instance = nil alors
Commencer
Hérité de création (Création de personnes réunies);
FreeOnTermiate: = true;
fin
autre
augmenter l'exception.createfmt ('ne peut pas créer plus d'une instance tsendthread!', [syserrorMessage (0)]);
fin;
destructor tsendthread.destroy;
Commencer
Hérité de détruire;
fin;
procédure tsendthread.Joke;
Commencer
fin;
procédure tsendthread.exécute;
var
The User: Tuserinfo;
TMPSQL: String;
Commencer
// Exécuter des tâches
// Traitement l'émission en temps opportun '+ gameinfo.mainUserable +'
tmppsql: = 'select * from' + mainusertable + 'where destre = 0 order by id';
Service1.adoquery_Send.Connection: = Conn_server;
SqlQuery (service1.adoquery_send, tmppsql);
tandis que (pas service1.adoquery_send.eof) et (non terminé) faire
Commencer
theUser.seqid: = '0';
theUser.uid: = '';
theUser.spc: = getmsg ('paramètre', 'spcode', theexename + '. ini');
theUser.Recordid: = '0';
theUser.mob: = Service1.adoquery_send.fieldByName ('mobile'). Asstring;
AutoJoke (The User);
Service1.adoquery_send.next;
fin;
Sommeil (600001);
Global_instance: = nil;
Mettre fin;
// La tâche est terminée
fin;
fin.