[Switch] The Delphi program can run only one instance in three ways:

Source: Internet
Author: User

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.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.