โหมด Singleton ใช้เพื่อ จำกัด วัตถุเดียวของคลาสที่แน่นอนในกระบวนการ ) หากมาถึงการสร้างเธรด แต่ถ้ามีนาฬิกาอื่นมาถึงก่อนที่เธรดนี้จะเสร็จสิ้นงานของมันมันอาจสร้างหลายเธรดเพื่อดำเนินการงานส่งผลให้เกิดความสับสนดังนั้นลองใช้โหมด Singleton เพื่อแก้ปัญหานี้ (แน่นอนว่ามีอื่น ๆ การแก้ปัญหา
รหัสหลักมีดังนี้:
// ตัวจับเวลา
ขั้นตอน tservice1.timer_maintimer (ผู้ส่ง: tobject);
วาจา
MyStringList: TSTRINGLIST;
SearchRec: TsearchRec;
ตอนนี้: สตริง;
เริ่ม
พยายาม
DateTimetOstring (ตอนนี้ 'HH: NN' ตอนนี้);
ถ้า leftstr (Nowtime, 4) = leftstr (getMSG ('game', 'เวลาออก', theExename+'. ini'), 4) แล้ว
เริ่ม
// สร้างเธรดการส่ง
global_instance: = tsendthread.getInstance;
-
จบ;
ยกเว้น
ใน E: ข้อยกเว้นทำ
เริ่ม
MyStringList: = tStringList.create;
ถ้า fileexists (extractFilePath (paramstr (0))+'err.txt') แล้ว
MyStringList.loadFromFile (ExtractFilePath (Paramstr (0))+'err.txt');
MyStringList.add ('('+DateTimetOSTR (ตอนนี้)+') [ข้อผิดพลาดในการสร้างเธรด:]'+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', ansireplacestr (extractFilePath (paramstr (0))+'err.txt', '. txt' ', ตอนนี้)+'. txt '));
จบ;
จบ;
จบ;
จบ;
จบ;
// หน่วยเธรด
UNIT_SEND;
ส่วนต่อประสาน
ใช้
Sysutils, ชั้นเรียน, strutils, หลัก;
พิมพ์
tsendthread = คลาส (tthread)
สาธารณะ
ตัวสร้างสร้าง (createSuspended: บูลีน);
Destructor ทำลาย;
ฟังก์ชั่นคลาส getinstance: tsendthread;
เรื่องตลกขั้นตอน;
ได้รับการคุ้มครอง
ขั้นตอนดำเนินการ;
จบ;
วาจา
Global_Instance: TsendThread;
การดำเนินการ
ใช้ db;
ฟังก์ชั่นคลาส tsendthread.getInstance: tsendthread;
เริ่ม
ถ้า global_instance = nil แล้ว
เริ่ม
global_instance: = tsendthread.create (เท็จ);
จบ;
ผลลัพธ์: = global_instance;
จบ;
constructor tsendthread.create (createSuspended: บูลีน);
เริ่ม
ถ้า global_instance = nil แล้ว
เริ่ม
สร้างมรดก (createSuspended);
Freeontermate: = true;
จบ
อื่น
เพิ่ม Exception.createFMT ('ไม่สามารถสร้างอินสแตนซ์ tsendthread ได้มากกว่าหนึ่งอินสแตนซ์!', [SyserRorMessage (0)]);
จบ;
destructor tsendthread.destroy;
เริ่ม
ทำลายที่สืบทอดมา;
จบ;
ขั้นตอน tsendthread.joke;
เริ่ม
จบ;
ขั้นตอน tsendthread.execute;
วาจา
Theuser: Tuserinfo;
TMPSQL: สตริง;
เริ่ม
// ดำเนินงาน
// การประมวลผลการออกในเวลาที่เหมาะสม '+gameinfo.mainusertable+'
tmpsql: = 'select * จาก'+mainusertable+'โดยที่ทำลาย = 0 ลำดับโดย id';
Service1.adoquery_send.connection: = conn_server;
sqlQuery (service1.adoquery_send, tmpsql);
ในขณะที่ (ไม่ใช่ service1.adoquery_send.eof) และ (ไม่สิ้นสุด) ทำ
เริ่ม
theuser.seqid: = '0';
theuser.uid: = '';
theuser.spc: = getMSG ('พารามิเตอร์', 'spcode', theExename+'. ini');
theuser.recordid: = '0';
theuser.mob: = service1.adoquery_send.fieldbyname ('มือถือ'). assstring;
autojoke (Theuser);
Service1.adoquery_send.next;
จบ;
นอนหลับ (600001);
global_instance: = nil;
สิ้นสุด;
// งานเสร็จสมบูรณ์
จบ;
จบ.