Delphi – singleton pattern implementation of the system under the mutex

Create a mutex function using CreateMutex

With Windows system function CreateMutex (), to find out whether the current instance of the specified process system already exists, if not create a mutex.

CreateMutex following function prototype:

function CreateMutex(lpMutexAttributes: PSecurityAttributes; bInitialOwner: BOOL; lpName: PChar): THandle; 

These parameters:

lpMutexAttributes: SECURITY_ATTRIBUTES structure type pointer may be NULL.

BInitialOwner: Whether to initialize the mutex.

lpName: the name mutex object body, usually the name of the project.

Eventually, the function returns a handle to the body are mutually exclusive.


Delphi WinFrm embodiment using single-mode mutex

Click Project, View Source;

uses to add Windows;

The following code between the Begin and End.




***************************** singleton ****************** **************

CreateMutex(nil, False, 'Application Name'); if GetLastError = ERROR_ALREADY_EXISTS then begin Application.MessageBox('

The system has been opened up, make sure the next!

', '


', MB_OK); Halt(0); end; //

***************************** singleton ****************** **************


Leave a Reply