C # How to run only one instance Program

Source: Internet
Author: User

A mutex process (Program), simply put, is to run only one instance of the program in the system. currently, many software have this function. For example, Maxthon can be set to "open only one form" or Bitcomet. I also saw this feature of these software to study this problem. to achieve program mutex, there are usually three methods, which are implemented in the C # language below:

Method 1: Use the thread mutex variable. Determine whether the instance is running by defining the mutex variable. C # implementation is as follows:

Change the Main () function in the program. cs file to the following code:

Static void Main ()
{
Bool runone;
System. Threading. Mutex run = new System. Threading. Mutex (true, "xinbiao_a_test", out runone );
If (runone)
{
Run. ReleaseMutex ();
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Application. Run (new Form1 ());
}
Else
{
MessageBox. Show ("an instance has been running. ");
}
}

Note: The program uses the statement System. threading. mutex run = new System. threading. mutex (true, "xinbiao_a_test", out runone); to declare a Mutex variable run, where "xinbiao_a_test" is the Mutex name, the Boolean runone variable is used to save whether the program has been run.

Implementation Method 2: Determine the process. Before running the program, we can check whether there is a process with the same name in the process. If the process is not running, if yes, it will not run. apply System. the main code is as follows:

1. Add the following functions to the program. cs file:

Public static System. Diagnostics. Process RunningInstance ()
{
System. Diagnostics. Process current = System. Diagnostics. Process. GetCurrentProcess ();
System. Diagnostics. Process [] processes = System. Diagnostics. Process. GetProcesses ();
Foreach (System. Diagnostics. Process process in processes) // query processes with the same name
{
If (process. Id! = Current. Id) // ignore the current process
{// Check whether the running location of the same process is the same.
If (System. Reflection. Assembly. GetExecutingAssembly (). Location. Replace ("/", @ "") = current. MainModule. FileName)
{// Return the other process instance.
Return process;
}
}
} // No other instance was found, return null.
Return null;
}

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

Static void Main ()
{
If (RunningInstance () = null)
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Application. Run (new Form1 ());
}
Else
{
MessageBox. Show ("an instance has been running. ");
}
}

Method 3: Perform the global atomic method. Before creating A program, check the global atomic table to see if there is A specific atomic A (added at the time of creation) and stop the creation when the atomic table exists, this indicates that the program has run an instance. If the program does not exist, it will run and want to add A specific atomic A to the global atomic table. Remember to release A specific atomic A when exiting the program, otherwise, the instance will be released after it is shut down. C # implementation:

1. Declare the WinAPI function interface:

[System. Runtime. InteropServices. DllImport ("kernel32.dll")]

Public static extern UInt32 GlobalAddAtom (String lpString); // Add an atom

[System. Runtime. InteropServices. DllImport ("kernel32.dll")]

Public static extern UInt32 GlobalFindAtom (String lpString); // search for Atoms

[System. Runtime. InteropServices. DllImport ("kernel32.dll")]

Public static extern UInt32 uglobaldeleteatom (UInt32 nAtom); // delete an atom

2. Modify the Main () function as follows:

Static void Main ()
& Nbs

Related Article

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.