C # implement so that the program can open only one instance (Conclusion 3 ))

Source: Internet
Author: User

Use c # To develop an application. Sometimes you only need to enable the application once ..
Three methods can be implemented. If you have any good methods, let's discuss them ..

 

// ===== Create a mutex method: ====
// Bool blnIsRunning;
// Mutex mutexApp = new Mutex (false, Assembly. GetExecutingAssembly (). FullName, out blnIsRunning );
// If (! BlnIsRunning)
//{
// MessageBox. Show ("the program is running! "," Prompt ",
// MessageBoxButtons. OK, MessageBoxIcon. Exclamation );
// Return;
//}

 

 

 

// Ensure that only one client is running
// System. Threading. Mutex mutexMyapplication = new System. Threading. Mutex (false, "OnePorcess.exe ");
// If (! MutexMyapplication. WaitOne (100, false ))
//{
// MessageBox. Show ("program" + Application. ProductName + "already running! ", Application. ProductName,
// MessageBoxButtons. OK, MessageBoxIcon. Error );
// Return;
//}

 

 

// ====== Process method: (execution can still be performed after the program name is modified) ====
// Process current = Process. GetCurrentProcess ();
// Process [] processes = Process. GetProcessesByName (current. ProcessName );
// Foreach (Process process in processes)
//{
// If (process. Id! = Current. Id)
//{
// If (process. MainModule. FileName
// = Current. MainModule. FileName)
//{
// MessageBox. Show ("the program is running! ", Application. ProductName,
// MessageBoxButtons. OK, MessageBoxIcon. Exclamation );
// Return;
//}
//}
//}

 

You only need to put the required method code in the Void Main () method ..

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.