There are many ways to implement a single-instance application. I will list some common methods:
1. Use mutex:
Static void Main ()
{
Bool isOnlyOne;
System. Threading. Mutex CTX = new System. Threading. Mutex
(False, "OnlyOne", out isOnlyOne );
If (isOnlyOne)
{
Application. Run (new Form1 ());
}
Else
{
MessageBox. Show ("the current application is running, and you do not need to run it again!
}
2. Use the Process class to check whether the application Process already exists:
Static void Main ()
{
Bool isOnlyOne;
Foreach (process p in Process. GetProcesses ())
{
If (p. ProcessName = "xxx ")
IsOnleOne = false;
}
If (isOnlyOne)
{
Application. Run (new Form1 ());
}
Else
{
MessageBox. Show ("the current application is running, and you do not need to run it again!
}
This method causes slow program startup, which is generally not recommended.
3. inherit from WindowsFormsApplicationBase to implement a single instance manager. Someone has already written this topic and I will not repeat it. For details, refer:
Http://hi.baidu.com/hetaoos/blog/item/2abda7185ab63a0334fa415f.html