-The program can start only one instance.

Source: Internet
Author: User

Sometimes, we need Program Only one instance can be started in the system. For example, Windows Medea player, a playback software provided by windows, can only start one instance in windows. The reason is simple. If you start several instances at the same time but play different files, the sound and image will cause confusion. In the design mode, there is a singleton mode, which means that the class has only one instance. (For more information about the singleton model, see my study notes on Rereading design patterns (3)-questions about the singleton model.)
For a program, we only need to check a setting when the program starts. If the program is not started, update the setting to that the program has started and then start the program normally. If the program has started, terminate the startup of the program. When the program exits, the setting is restored to that the program is not started. Based on the above ideas, we can easily come up with the following two methods:
I. File Method
Create a file on the hard disk and set a value in the file to determine whether the program has been started.
Ii. Registry Method
Create a key in the registry and decide whether to start the program based on the key value of the key.
However, the above two methods have I/O operations. I don't think this is the best way. The following describes two methods without I/O operations. The idea is the same as above. When a process starts, it checks whether a certain setting continues to start the process. To determine whether the same program has started an instance, that is to say, there will be two processes accessing the same setting, so this setting should allow process access, for example, the file and registry in the above two methods. When using VC for development, we can also use file ing and mutex. The following is a detailed description:
When creating a project, VC automatically creates an app class. For example, if your project name is starlee, the class name of this app class is cstarleeapp. The initinstance () and exitinstance () Methods of the class are called respectively during start and exit of the process (). So, our Code These two methods are added.
Iii. File ing
First, add a member variable to the app class:

Handle m_hfilemapping;

Then, add the following code at the beginning of the initinstance () method of the app class:

M_hfilemapping = Createfilemapping (null, null, page_readonly, 0 , 13 , " Starlee " );

//Check whether filemapping has been created
//If the process has been created, terminate the process startup.
If(M_hfilemapping! =Null)&&(Getlasterror ()=Error_already_exists ))
{
Closehandle (m_hfilemapping );

MessageBox (null,"This process has been started","Error", Mb_ OK );

ReturnFalse;
}

Finally, add the following code to the exitinstance () method of the app class:

If (M_hfilemapping ! = Null)
Closehandle (m_hfilemapping );

Iv. mutex Method
First, add a member variable to the app class:

Handle m_hmutex;

Then, add the following code at the beginning of the initinstance () method of the app class:

M_hmutex = Createmutex (null, true, " Starlee " );

//Check whether mutex has been created
//If the process has been created, terminate the process startup.
If(M_hmutex! =Null)&&(Getlasterror ()=Error_already_exists ))
{
Releasemutex (m_hmutex );

MessageBox (null,"This process has been started","Error", Mb_ OK );
 
ReturnFalse;
}

Finally, add the following code to the exitinstance () method of the app class:

If (M_hmutex ! = Null)
{
Releasemutex (m_hmutex );
Closehandle (m_hmutex );
}

The idea of the above two methods is the same as the steps for adding code. Of course, the effect is the same. Selecting any method can enable the process to start only one instance.

I published my article "How to start only one instance" and was recommended to the csdn homepage. Many netizens have read this article and made some good suggestions. One of the netizens said they could use the shared variables method. I collected some information online and tested the code. Now I want to add this method:
V. Shared Variables
First, add the following code at the beginning of the CPP file of the app class:

# Pragma Data_seg ("starlee ") // Custom Data Segment
Char Ninstancecount =   - 1 ; // Number of instances. The following describes the benefits of using Char to define the number of instances.
# Pragma Data_seg ()
# Pragma Comment (linker, "/section: starlee, RWS ") // Share this data segment (R-read, W-write, S-share)

Then, add the following code at the beginning of the initinstance () method of the app class:

Ninstancecount ++ ;
If (Ninstancecount >   0 )
{
MessageBox (null, " This process has been started " , " Error " , Mb_ OK );

Return False;
}

Finally, add the following code to the exitinstance () method of the app class:

Ninstancecount --;

PS: Here is a tip for defining the number of instances as char in the code above. We know that the length of char is 1 byte, which is-128 ~ An integer between 127. The maximum value of our process instance is 1, and the fewer things we add in the Data Segment of the program, the better, so char becomes the best choice.

In more than a dozen replies to the article "how to start a program with only one instance", many netizens have mentioned that if the process is accidentally terminated, the code for restoring the settings will not be called, then the program cannot be started again. Therefore, I tested each method and analyzed the results below:
Test environment:
Windows 2000, VC ++ 6.0
Test method:
1. Start the program, terminate the process in the task manager, and start the process again.
2. Delete the code added to the exitinstance () method of the app class. After compilation, start the program, close the program, and start the program again.
Result:
For method 1 and method 2 (file method and registry method), due to I/O operations, if the code to restore the settings is not called, unless you manually restore the settings, otherwise, the program will never be started again. This is also the main reason why I do not recommend these two methods.
For the last three methods (File ing, mutex, and shared variable), even if the process is terminated abnormally, the code for restoring the settings will not be called and the program will not be restarted. I think this is because these three types of objects (File ing, mutex, shared variables) can be accessed by exaggerated processes, but they still belong to the Creator process, it will be destroyed with the Creator's destruction, and Windows will release it when the process ends (whether normal or unexpected.
In addition, I found the following sentence in msdn's introduction to createmutex:
The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
It can be said that this clearly tells us that the mutex will be automatically released when the program is terminated.
Therefore, among the five methods I have introduced that allow a process to start only one instance, Mutex Method Is the best solution.

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.