It is sometimes necessary to run the program only once, that is, only one instance in the memory at the same time. Some programs run on multiple instances, for example, the dictionary Program (Kingsoft ). This article describes how to implement a single instance running Windows in different languages.
C/C ++
Windows program has four entry functions: Main, wmain, winmain, and wwinmain. When the entry function determines the program, an instance (process) is running, here we need to use the kernel object shared between processes: mutex, and relevant information will be written to the Registry. The sample code is as follows:
- Int winapi winmain (hinstance, hinstance, lpstr lpsz1_line, int ncmdshow)
- {
- : Setlasterror (no_error); // ensure that the returned value of getlasterror is not disturbed.
- : Createmutex (null, false, "nutexname ");
- If (: getlasterror () = error_already_exists)
- {
- // Return;... an instance of the program is running.
- }
- // Execute the program for the first time.
- }
Note: createmutex is a system API provided by windows. If another program is created after an instance of the program is created, the system fails to set the error code
Error_already_exists. Therefore, this error code is returned when you use another API function getlasterror to check whether an instance is running.
C #
In C #, you can view the number of processes with the specified name to determine the number of program instances. The instance code is as follows:
- String processname = system. Diagnostics. process. getcurrentproce (). processname;
- System. Diagnostics. Process [] processes = system. Diagnostics. process. getprocessesbyname (processname );
- If (processes. length> 1)
- {
- // Return;... an instance of the program is running.
- }
- // Execute the program for the first time.
Java
C/C ++ Java and C # Use the features of the Windows platform to implement a single-instance program. However, Java's platform independence makes it difficult to use these features. Using the semaphore idea, we can find a replacement similar to a semaphore in Java program.
Method 1: Use Files. When running the program, check whether a specific file exists. If the file does not exist, it indicates that the program is running and the file is deleted after the program exits, similar to the release of the semaphore object. If another instance of the program runs and the file already exists, it indicates that the existing instance is running and exits. The disadvantage of this method is that the program may not be able to delete the identification file when it exits due to an exception, and the program will not be able to run in the future.
Method 2: because the file may not be deleted after the program exits, you can use the port number to replace the semaphore. When the program starts, check whether the port number is occupied. If not, establish a connection to the port, release the port number after the program exits. When you check that the port number is occupied, it indicates that an instance is running and the instance exits. The defect of this method is that the port number may be occupied by other programs, leading to misjudgment and cannot start the program. Therefore, avoid conflicts when selecting port numbers.
Since Java is difficult to have shared variables available between processes, the above methods are flawed. To reduce the error rate, you can share the two methods, that is, only when the file exists and the port number is occupied can an instance be considered to be running. This can greatly reduce the possibility of misjudgment.