The winform program restricts only one process to be opened.

Source: Internet
Author: User

 

There are many solutions. First, the most silly one:
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
If (System. Diagnostics. Process. GetProcessesByName (System. Diagnostics. Process. GetCurrentProcess (). ProcessName). Length> 1)
{
MessageBox. Show ("one already runs ");
}
Else
{

Application. Run (new Form1 ());
}

}
}

 

 

 

 

 

Another type

Static class Program
{
[STAThread ()]
Public static void Main ()
{
Bool ret;
System. Threading. Mutex m = newSystem. Threading. Mutex (true, Application. ProductName, out ret );
If (ret)
{
// System. Windows. Forms. Application. EnableVisualStyles (); // these two lines implement the XP visual style.
// System. Windows. Forms. Application. DoEvents ();
System. Windows. Forms. Application. Run (new Form1 ());
// FrmMain is the main form of your program. This is not required for console programs.
M. ReleaseMutex ();
}
Else
{
MessageBox. Show (null, "one application with the same program is already running. Please do not run multiple programs at the same time. The program \ n is about to exit. ", Application. ProductName, MessageBoxButtons. OK, MessageBoxIcon. Warning );
// Message, which can be deleted.
Application. Exit (); // Exit the program
}
}
}

 

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.