This article will tell you how to use C# to create an instance of the program that can only run in the system.
To implement program mutual exclusion, there are usually the following ways, which are implemented in C# language:
Method 1:
Use thread mutex variables. Define mutex variables to determine whether an instance has been run.
Change the Main() function in the file to the following code:
using System;
using ;
using ;
namespace NetTools
{
static class Program
{
[DllImport("")]
private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
[DllImport("")]
private static extern bool FlashWindowEx(int pfwi);
/// <summary>
/// The main entry point of the application.
/// </summary>
[STAThread]
static void Main()
{
bool runone;
run = new (true, "single_test", out runone);
if (runone)
{
();
();
(false);
FrmRemote frm = new FrmRemote();
int hdc = .ToInt32(); // write to ...
(frm);
IntPtr a = new IntPtr(hdc);
}
else
{
("A instance has been run.");
//IntPtr hdc = new IntPtr(1312810); // read from...
//bool flash = FlashWindow(hdc, true);
}
}
}
}
Description: In the program, a mutex variable run is created by the statement run = new (true, "single_test", out runone); where "single_test" is the mutex name. When returning this method, if a local mutex or a specified naming system mutex is created, the Boolean runone is true; if the specified naming system mutex already exists, it is false. Named mutexes are system-wide.
Method 2:Using the method of judging the process, we search for whether there are processes with the same name in the process before running the program, and at the same time the running position is the same. If the program is not run, it will not run if there is. In C#, we use the Process class in the name space to implement it.The main code is as follows:
1. Add the function to the file as follows:
public static RunningInstance()
{
current = ();
[] processes = ();
foreach ( process in processes) //Find processes with the same name
{
if ( != ) //Ignore the current process
{ //Confirm whether the running location of the program of the same process is the same.
if (().("/", @"/") == )
{ //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)
{
();
(false);
(new Form1());
}
else
{
("A instance has been run.");
}
}
Method 3:Global atomic method. Before creating a program, check whether a specific atom A exists in the global atom table (added at creation). Stop creating when it exists, indicating that an instance of the program has been run; if it does not exist, run the program and want to add a specific atom A to the global atom table; remember to release a specific atom A when exiting the program, otherwise it will not be released until it is shut down. C# is implemented as follows:
1. Declare WinAPI Function Interface
[("")]
public static extern UInt32 GlobalAddAtom(String lpString); //Add atoms
[("")]
public static extern UInt32 GlobalFindAtom(String lpString); //Find atom
[("")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); //Delete atom
2. Modify the Main() function as follows:
static void Main()
{
if (GlobalFindAtom("jiaao_test") == 77856768) //The atom "jiaao_test" was not found
{
GlobalAddAtom("jiaao_test"); //Add atom "jiaao_test"
();
(false);
(new Form1());
}
else
{
("A instance has been run.");
}
}
3. Add the following code to the FormClosed event:
GlobalDeleteAtom(GlobalFindAtom("jiaao_test"));//Delete the atom "jiaao_test"
--------------------------------------*-------*--------*-----------------------------------------------
The above is the basic and general idea of creating a mutually exclusive program. I personally believe that the first method is the simplest.
To implement program mutual exclusion, there are usually the following ways, which are implemented in C# language:
Method 1:
Use thread mutex variables. Define mutex variables to determine whether an instance has been run.
Change the Main() function in the file to the following code:
Copy the codeThe code is as follows:
using System;
using ;
using ;
namespace NetTools
{
static class Program
{
[DllImport("")]
private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
[DllImport("")]
private static extern bool FlashWindowEx(int pfwi);
/// <summary>
/// The main entry point of the application.
/// </summary>
[STAThread]
static void Main()
{
bool runone;
run = new (true, "single_test", out runone);
if (runone)
{
();
();
(false);
FrmRemote frm = new FrmRemote();
int hdc = .ToInt32(); // write to ...
(frm);
IntPtr a = new IntPtr(hdc);
}
else
{
("A instance has been run.");
//IntPtr hdc = new IntPtr(1312810); // read from...
//bool flash = FlashWindow(hdc, true);
}
}
}
}
Description: In the program, a mutex variable run is created by the statement run = new (true, "single_test", out runone); where "single_test" is the mutex name. When returning this method, if a local mutex or a specified naming system mutex is created, the Boolean runone is true; if the specified naming system mutex already exists, it is false. Named mutexes are system-wide.
Method 2:Using the method of judging the process, we search for whether there are processes with the same name in the process before running the program, and at the same time the running position is the same. If the program is not run, it will not run if there is. In C#, we use the Process class in the name space to implement it.The main code is as follows:
1. Add the function to the file as follows:
Copy the codeThe code is as follows:
public static RunningInstance()
{
current = ();
[] processes = ();
foreach ( process in processes) //Find processes with the same name
{
if ( != ) //Ignore the current process
{ //Confirm whether the running location of the program of the same process is the same.
if (().("/", @"/") == )
{ //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:
Copy the codeThe code is as follows:
static void Main()
{
if(RunningInstance()==null)
{
();
(false);
(new Form1());
}
else
{
("A instance has been run.");
}
}
Method 3:Global atomic method. Before creating a program, check whether a specific atom A exists in the global atom table (added at creation). Stop creating when it exists, indicating that an instance of the program has been run; if it does not exist, run the program and want to add a specific atom A to the global atom table; remember to release a specific atom A when exiting the program, otherwise it will not be released until it is shut down. C# is implemented as follows:
1. Declare WinAPI Function Interface
Copy the codeThe code is as follows:
[("")]
public static extern UInt32 GlobalAddAtom(String lpString); //Add atoms
[("")]
public static extern UInt32 GlobalFindAtom(String lpString); //Find atom
[("")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); //Delete atom
2. Modify the Main() function as follows:
Copy the codeThe code is as follows:
static void Main()
{
if (GlobalFindAtom("jiaao_test") == 77856768) //The atom "jiaao_test" was not found
{
GlobalAddAtom("jiaao_test"); //Add atom "jiaao_test"
();
(false);
(new Form1());
}
else
{
("A instance has been run.");
}
}
3. Add the following code to the FormClosed event:
GlobalDeleteAtom(GlobalFindAtom("jiaao_test"));//Delete the atom "jiaao_test"
--------------------------------------*-------*--------*-----------------------------------------------
The above is the basic and general idea of creating a mutually exclusive program. I personally believe that the first method is the simplest.