1. Create a mutex object
Create a mutex object in project project1.dpr
Program project1
Uses
Windows, form,
Frmmain in 'frmmain. pa' {mainform };
{$ R *. Res}
VaR
Happmutex: thandle; // declare the mutex variable
Begin
Happmutex: = createmutex (nil, false, 'projectname'); // create a mutually exclusive object projectname project name
If (happmutex <> 0) and (getlasterror () = error_already_exists) then
Begin // happmutex <> 0 indicates that the mutex object is successfully created.
MessageBox ('the program is running. Close this window according to OK! ',' Prompt! ', Mb_ OK );
End
Else
Begin
Application. ininitialize;
Application. createform (tmainform, mainform );
Application. Run;
End;
Releasemutex (happmutex); // closes the closehandle (happmutex) of the mutex.
End.
Ii. window searching
This is the simplest method. Before running the program, use the findwindow function to find a window with the same class name and title. If yes, it indicates that the window already exists.
Add the following code in the initialization part of the project source file:
Program project1
Uses
Forms, windows;
VaR hwnd: thandle;
Begin
Hwnd: = findwindow ('tform1 ', 'form1 ');
If hwnd = 0 then
Begin
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
End
Else
Application. MessageBox (pchar ('this program is already running! '), Pchar (' prompt! '), Mb_ OK );
End;
The findwindow () function has two parameters (class name and title window). One of the parameters can be ignored. However, I strongly recommend that you use both parameters, so that other programs are using the same class name, they cannot get the correct result.
In addition, if the program is run in the Delphi ide window, it will not be run once, because there is already a window with the same class name and title: The Window port at design time.
Iii. Global atomic method (when the program ends abnormally, this method cannot determine whether the program has exited)
We can also add global atoms to the system to prevent the running of multiple program instances. The global atom is maintained by the Windows system. It ensures that each atom is unique and manages its reference count. When the reference count of the global atom is 0, clear from memory. We use the globaladdatom function to add a string of less than 255 bytes to the global atom and use globalfindatom to check whether the global atom exists, at the end of the program, use the globaldeleteatom function to delete the added global atom. Example:
Program project1
Uses Windows, froms,
Unit1 in 'unit1. pa' {form1 },
Const iatom = 'application'; // can be any atomic weight that uniquely identifies the program
Begin
If globalfindatom (iatom) = 0 then
Begin
Globaladdatom (iatom); // Add a global atom
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
Globaldeleteatom (globalfindatom (iatom); // Delete the added global atom
End
Else
Application. MessageBox (pchar ('this program is already running! '), Pchar (' prompt! '), Mb_ OK );
End.