Allow the program to run only one instance

Source: Internet
Author: User

A typical feature of Windows is multi-task. You can open multiple windows at the same time or
Run multiple instances of the program at the same time. For example, you can open multiple resource managers to perform file mobile copy operations. However
For some reason (such as security), we need to make some restrictions so that the program can only run one instance. In de
In lphi programming, I have summarized the following methods:


I. window searching

This is the simplest method. Use the findwindow function to find the same window class name and tag before running the program.
The question window. If it is found, it indicates that an instance already exists. Add the following generation in the initialization part of the project source file
Code:

Program oneapp

Uses
Forms, windows; (the methods described here must be added to the project source file, and will not be repeated in the future)
  
VaR
Hwnd: thandle;
Begin
Hwnd: = findwindow ('tform1 ', 'singleapp ');
If hwnd = 0 then
Begin
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
End;
End;

The findwindow () function has two parameters, one of which can be ignored. However, I strongly recommend that you set the two parameters
So that other programs are using the same class name and cannot get the correct result. In addition
If you run this program in the Delphi ide window, it cannot be run at one time because a window with the same class name and title already exists:
The design form.


Ii. Use mutex objects
If you think that the window search method is not efficient, you can use the method of creating mutex objects. Although mutex
It is usually used for Synchronous connections, but it is very convenient to use it in this place. It is easy to use only four sentences of code.

VaR
Mutex: thandle;
Begin
Mutex: = createmutex (nil, true, 'singleapp ');
If getlasterror <> error_already_exists then // if another instance does not exist
Begin
Application. createhandle;
Application. createform (texpnoteform, expnoteform );
Application. Run;
End;
Releasemutex (mutex );
End.


Iii. Global atomic Method

We can also add global atoms to the system to prevent the running of multiple program instances. Global atomic
The Windows system is responsible for maintaining, it can ensure that each atom is unique, manage its reference count, and when
When the reference count of the global atom is 0, it is cleared from the memory. We use the globaladdatom function to add
Use globalfindatom to check whether the global atom exists.
Use the globaldeleteatom function to delete the added global atom. Example:

Uses
Windows;
  
Const iatom = 'singleapp ';
  
Begin
If globalfindatom (iatom) = 0 then
Begin
Globaladdatom (iatom );
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
Globaldeleteatom (globalfindatom (iatom ));
End
Else
MessageBox (0, 'you can not run a second copy of this app', '', mb_ OK );
End.

Using global atomic reference counting rules, we can also determine the number of instances currently running the program:

VaR
I: integer;
Begin
I: = 0;
While globalfindatom (iatom) <> 0 do
Begin
Globaldeleteatom (globalfindatom (iatom ));
I: = I + 1;
End;
Showmessage (inttostr (I ));
End;

The above methods are adopted in the author's Delphi 4.0 and Windows 95.

Allow the program to run only one instance

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.