I write a c# windorm application and generate a install file, when I finish the installation, every time I double click the shortcut in desktop, it will open a new window, does anyone can tell me how could I just open the original window when I double click the shortcut in desktop?
            Asked
            
        
        
            Active
            
        
            Viewed 1,523 times
        
    0
            
            
        - 
                    possible duplicate of [this](http://stackoverflow.com/questions/6486195/ensuring-only-one-application-instance) which is a duplicate of [What is the correct way to create a single instance application?](http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application) – Pierre-Luc Pineault Apr 28 '14 at 05:33
1 Answers
3
            I prefer a mutex solution similar to the following. As this way it re-focuses on the app if it is already loaded
using System.Threading;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
   bool createdNew = true;
   using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
   {
      if (createdNew)
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new MainForm());
      }
      else
      {
         Process current = Process.GetCurrentProcess();
         foreach (Process process in Process.GetProcessesByName(current.ProcessName))
         {
            if (process.Id != current.Id)
            {
               SetForegroundWindow(process.MainWindowHandle);
               break;
            }
         }
      }
   }
}
It gives focus to your application window if it is already running.
 
    
    
        Vimes
        
- 10,577
- 17
- 66
- 86
 
    
    
        Anant Dabhi
        
- 10,864
- 3
- 31
- 49
