This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22992|Reply: 2

[Source] Detailed explanation of several ways to solve the problem of a C# program that only allows one instance to run

[Copy link]
Posted on 12/8/2016 1:11:13 PM | | |

Method 1:
Use thread mutex variables. Define mutex variables to determine whether an instance is running.
Change the Main() function in the program.cs file to the following code:




Note: In the program, the statement System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone); to create a mutex variable run, where "single_test" is the mutex name, and when this method returns, if a local mutex or a specified naming system mutex is created, the boolean value runone is true; false if the specified naming system mutexe already exists. Named mutexes are system-wide.
Method 2: Use the way of judging the process, before we run the program, we find whether there is a process with the same name in the process, and the running position is also the same process. Apply the Process class in the System.Diagnostics namespace in C# to implement it, the main code is as follows:
1. Add a function to the program.cs file as follows:




2. Change the Main () function to the following code:




Method 3: Global atomic method, before creating a program, first check the global atomic table to see if there is a specific atom A (added at the time of creation), and stop creating it when it exists, indicating that the program has run an instance; If it doesn't exist, run the program and want to add a specific atom A to the global atomic table; When exiting the program, remember to release a specific atom A, otherwise it will not be released until the computer is turned off. The C# implementation is as follows:
1. Declare the WinAPI function interface

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern UInt32 GlobalAddAtom(String lpString); Add atoms
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern UInt32 GlobalFindAtom(String lpString); Find atoms
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); Delete atoms
2. Modify the Main() function as follows:




3. Add the following code to the FormClosed event:
GlobalDeleteAtom(GlobalFindAtom("jiaao_test")); Delete atomic "jiaao_test"
--------------------------------------*-------*--------*-----------------------------------------------
The above is the basic general idea of creating a mutually exclusive program, and I personally think that the first method is the simplest.




Previous:Difference between Directory.GetCurrentDirectory and Application.StartupPath
Next:The difference between .Net Framework4 and .Net Framework4.0 client profile
Posted on 7/10/2017 6:31:21 AM |
Thank you for sharing
 Landlord| Posted on 7/23/2024 2:21:04 PM |
C# programs only allow one instance to run the correct code
https://www.itsvse.com/thread-3391-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com