`

C# winform 应用程序只打开一次(实现)

    博客分类:
  • C#
 
阅读更多

winform 有的时候只能打开一次,下一次不要打开的应用

 

下面是code

 

 static class ApplicationStart
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool ExisFlag = false;
            System.Diagnostics.Process currentProccess = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] currentProccessArray = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process p in currentProccessArray)
            {
                if (p.ProcessName == currentProccess.ProcessName && p.Id != currentProccess.Id)
                {
                    ExisFlag = true;
                }
            }

            if (ExisFlag)
            {
                return;
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ModelFileGenerator.View.×××());
            }
        }
    }

 

上面是第一种方案

 

在来看看第二种方案

 

 

/// <summary>
        /// window form show count
        /// </summary>
        private const int WS_SHOWNORMAL = 1;

        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("kernel32.dll")]
        private static extern IntPtr LoadLibrary(string sLibName);

        /// <summary>
        /// application start
        /// </summary>
        public static void ApplicationStart()
        {
            Process instance = RunningInstance();
            if (instance == null)
            {
                System.Windows.Forms.Application.EnableVisualStyles();
                System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ×××());
            }
            else
            {
                HandleRunningInstance(instance);
            }
        }

        /// <summary>
        /// application repeat start
        /// </summary>
        public static void ApplicationRepeatStart()
        {
            Process instance = RunningInstance();
            if (instance != null)
            {
                HandleRunningInstance(instance);
            }
        }

        /// <summary>
        /// Running process instance
        /// </summary>
        /// <returns>the running process</returns>
        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            ////Loop through the running processes in with the same name
            foreach (Process process in processes)
            {
                ////Ignore the current process
                if (process.Id != current.Id)
                {
                    ////Make sure that the process is running from the exe file.
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        ////Return the other process instance.
                        return process;
                    }
                }
            }
            ////No other instance was found, return null.
            return null;
        }

        /// <summary>
        /// handle running instance
        /// </summary>
        /// <param name="instance">the running process</param>
        public static void HandleRunningInstance(Process instance)
        {
            ////Make sure the window is not minimized or maximized
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
            ////Set the real instance to foreground window
            SetForegroundWindow(instance.MainWindowHandle);
        }

 Main函数

 public const string APP_NAME = "×××";
        private static bool isFirstAppInstance = true;
        private static Mutex mutex = new Mutex(true, APP_NAME, out isFirstAppInstance);
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (isFirstAppInstance)
            {
                    ApplicationManagerUtil.ApplicationStart();
            }
            else
            {
                   ApplicationManagerUtil.ApplicationRepeatStart();
            }
        }

 

好了 如果有什么问题请和我联系

 

 

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics