Only one instance _ single open if the program has been opened again to start the main program of the current display




Reference https://www.jb51.net/article/75156.htm



the using the System.Runtime.InteropServices;
 the using the System.Diagnostics;
 the using the System.Reflection; namespace the System 
{ / * 
     * only one instance single open _ _ 
     * The https://www.jb51.net/article/75156.htm 
     * 
     * 
     * direct replacement method using 
        static void the Main () { 
            Application.EnableVisualStyles (); 
            Application.SetCompatibleTextRenderingDefault (to false); 
            the System.Diagnostics.Process P = OneAppStartup.GetInstance; 
            IF (P == null) { 
               // object program 
                Form1 frm the Form1 new new = (); 
                the Application.Run (new new the Form1 ());



    




            The else {} 
                OneAppStartup.ShowInstance (P); 
            } 
        } 

     * 
     * 
     * / 
    class OneAppStartup 
    { /// <Summary> /// This function sets the window is generated by the display state of different threads. /// </ Summary> /// <param name = "the hWnd"> window handle </ param> /// <param name = "for ncmdshow"> Specifies how the display window. View list of allowed values, please refer to the Description section ShowWlndow function. </ param> /// <Returns> visible if the original function, the return value is zero; if the originally hidden function, the return value is zero. </ Returns> 
        [the DllImport ( " the User32.dll ")]
         Private static extern BOOL the ShowWindowAsync (IntPtr the hWnd, int for ncmdshow);
        
        
        
        
        
        
           
        /// This function creates a thread sets the specified window to the foreground and activate the window. Steering the keyboard input window, and for the user to change various visual mark. Foreground window system to create a thread assigned permissions slightly higher than other threads. 
        /// </ Summary> 
        /// <param name = "the hWnd"> will be activated and transferred to the handle of the foreground window. </ param> 
        /// <Returns> If the window is set into the front, the return value is zero; if the front window is not set, the return value is zero. </ Returns> 
        [( "the DllImport the User32.dll ")]
         Private  static  extern  BOOL the SetForegroundWindow (IntPtr the hWnd);
         Private  const  int WS_SHOWNORMAL =. 1; /// <Summary> /// Gets a running instance, is not running instance return null; /// </ Summary> public static Process the GetInstance 
        { GET {


        
        
        
         
             
                Process Current = Process.
                Process[] processes = Process.GetProcessesByName(current.ProcessName);
                foreach (Process process in processes) {
                    if (process.Id != current.Id) {
                        if (Assembly.GetExecutingAssembly().Location.Replace("/","\\") == current.MainModule.FileName) {
                            return process;
                        }
                    }
                }
                return null;
            }

        }

        /// <summary>
        /// 显示已运行的程序。
        /// </summary>
        public static  void ShowInstance (Process instance) 
        { 
            the ShowWindowAsync (instance.MainWindowHandle, WS_SHOWNORMAL); // display, comment out 
            the SetForegroundWindow (instance.MainWindowHandle);       // put distal 
        } 
    } 
}

Guess you like

Origin www.cnblogs.com/xe2011/p/12606308.html
Recommended