Three solutions for implementing single-instance applications

Source: Internet
Author: User
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

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.