Desktop Application Instance Check

First, we install the desktop application on our computer. After successful installation, there will be a shortcut icon on the desktop, as shown in the below figure.
 
Desktop Application Instance Check
 
We can open the application multiple times with multiple clicks on that icon. You can look at the below figure.
 
Desktop Application Instance Check
 
For preventing or the application from opening multiple times, we need to write the following codes. Please write this code in Program.cs.
 
Program.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6. using System.Windows.Forms;  
  7. using DomainEntities;  
  8. using System.Xml;  
  9. using CCMS.Forms;  
  10. using System.Threading;  
  11. namespace CCMS {  
  12.     static class Program {  
  13.         /// <summary>  
  14.         /// The main entry point for the application.  
  15.         /// </summary>  
  16.         [STAThread]  
  17.         static void Main() {  
  18.             try {  
  19.                 const string APPGUID = "{9F6F0AC4-B9A1-90AB-DE0F-72F04E6BDE8F}";  
  20.                 using(Mutex mutex = new Mutex(false, APPGUID)) {  
  21.                     if (!mutex.WaitOne(0, false)) {  
  22.                         MessageBox.Show("Already Running Application!""An instance of the application is already running...", MessageBoxButtons.OK, MessageBoxIcon.Stop);  
  23.                         return;  
  24.                     }  
  25.                     Application.Run(new LoginPage());  
  26.                 }  
  27.             } catch (Exception ex) {  
  28.                 MessageBox.Show(ex.Message.ToString(), "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  29.             }  
  30.         }  
You can replace APPGUID with your own.
 
 Desktop Application Instance Check
 
We will get this type of warning after trying to open the application multiple times if it is already opened.
 

Conclusion

 
We learned how to prevent an application from opening multiple times if an instance of the application is already opened. Thank you.