Delphi implements Singleton mode

xiaoxiao2021-03-06  40

Single-case mode is used to limit only one class object in the process. The Singleton of this example is a thread instance. When each clock arrives, it detects whether to reach a moment (in the INI file of this example), if The arrival will generate a thread, but if it reaches a clock before this thread is completed, it is possible to generate multiple thread execution tasks, so that there is confusion, so consider using the Singleton mode to solve this problem (of course there are other solutions) However, in this case Singleton is used.

The core code is as follows:

// Timer unit

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', 'Ship', Theexename '. INI', 4) THEN

Begin

// Create a sending thread

Global_instance: = tsendthread.getInstance;

//

END;

Except

ON E: Exception DO

Begin // Capture Error Deposit TXT file

MyStringList: = TSTRINGLIST.CREATE;

IF FileExists (ExtractFilePath (paramstr (0)) 'Err.txt') THEN

MyStringList.LoadFromFile (ExtractFilePath) 'Err.txt';

MyStringList.Add ('(' DateTimetostr (now) ') [Create a thread error:]' E.MESSAGE);

MYSTRINGLIST.SAVETOFILE (ExtractFilePath) 'Err.txt';

MyStringList.Free;

IF Findfirst (ExtractFilePath (paramstr (0)) 'Err.txt', FaanyFile, Searchrec) = 0 THEN

Begin

If SearchRec.size> 5000000 Then

Begin

Renamefile (PARAMSTFILEPATH (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;

// thread unit

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;

ProtectedProcedure Execute; Override;

END;

VAR

Global_instance: tsendthread;

IMPLEMentation

Uses db;

Class function tsendthread.getinstance: tsendthread;

Begin

IF global_instance = nil dam

Begin

Global_instance: = tsendthread.create (false);

END;

Result: = Global_Instance;

END;

Constructor tsendthread.create (createSuspended: boolean);

Begin

IF global_instance = nil dam

Begin

Inherited Create (createSuspend);

FreeOnterminate: = true;

end

Else // If someone is accidentally created multiple creating objects, an exception is generated.

Raise Exception.createfmt ('Can Not Create More Than One Tsendthread Instance! ", [SySerrorMessage (0)]);

END;

Destructor tsendthread.destroy;

Begin

Inherited destroy;

END;

Procedure tsendthread.execute;

VAR

Theuser: tusefo;

TMPSQL: String;

Begin

// Execute the task

// Treatment timing down ' gameinfo.mainusertable '

TMPSQL: = 'SELECT * from' Mainusertable 'Where design';

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;

//mission completed

END;

End.

----------------------------------------- Why is it like? One sand gull in the world.

转载请注明原文地址:https://www.9cbs.com/read-73319.html

New Post(0)