C # create a mutex program _ only one program instance can be run

Source: Internet
Author: User

C # create a mutex process (Program) A mutex process (Program). Simply put, only one instance of the program can run in the system. Currently, many software have this function.

To achieve program mutex, there are usually the following methods:

Method 1:

Use the thread mutex variable to determine whether an instance is running by defining the mutex variable.
Change the Main () function in the program. cs file to the following code:

Code
 Using System;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;

Namespace NetTools
{
Static class Program
{

[DllImport ("user32.dll")]
Private static extern bool FlashWindow (IntPtr hWnd, bool bInvert );

[DllImport ("user32.dll")]
Private static extern bool flash1_wex (int pfwi );

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Bool runone;
System. Threading. Mutex run = new System. Threading. Mutex (true, "single_test", out runone );
If (runone)
{
Run. ReleaseMutex ();
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
FrmRemote frm = new FrmRemote ();
Int hdc = frm. Handle. ToInt32 (); // write...
Application. Run (frm );
IntPtr a = new IntPtr (hdc );
}
Else
{
MessageBox. Show ("an instance has been running. ");
// IntPtr hdc = new IntPtr (1312810); // read from...
// Bool flash = FlashWindow (hdc, true );
}

}
}
}

Note: The program uses the statement System. threading. mutex run = new System. threading. mutex (true, "single_test", out runone); Create a Mutex variable run, where "single_test" is the Mutex name. When this method returns, if a local mutex is created or the specified name system mutex is created, the Boolean value runone is true. If the specified name system mutex already exists, the value false. The named mutex is within the system range.

Method 2: Determine the process. Before running the program, check whether a process with the same name exists in the process and run the same path. 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:

Code

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.