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

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();
            }
        }

 

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

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C知道:C# WinForm应用程序是使用C#语言和WinForms框架构建的Windows桌面应用程序。下面是一些关于C# WinForm应用程序的基本概念和特点: 1. 窗体(Form):WinForm应用程序的主要界面是窗体,它是用户与应用程序进行交互的主要界面元素。你可以在窗体上添加各种控件来构建用户界面,如按钮、文本框、标签等。 2. 控件(Controls):WinForm提供了一系列预定义的控件,用于构建用户界面。你可以将这些控件拖放到窗体上,并通过属性窗口来设置它们的属性和行为。一些常见的控件包括按钮(Button)、文本框(TextBox)、标签(Label)、列表框(ListBox)等。 3. 事件处理(Event Handling):WinForm应用程序通过事件处理来响应用户的操作。你可以为控件的事件(如按钮点击事件)编写事件处理程序,当事件发生时,相应的代码将被执行。例如,你可以在按钮点击事件中执行某个操作或打开另一个窗体。 4. 数据绑定(Data Binding):WinForm支持简单的数据绑定,使你能够将数据源与控件进行绑定,从而实现数据的显示和更新。你可以使用数据绑定来将数据库、集合或其他数据源中的数据显示在控件中,或者将用户输入的数据保存到数据源中。 5. 对话框(Dialogs):WinForm提供了一些预定义的对话框,用于与用户进行交互,如消息框、文件选择框、颜色选择框等。你可以使用这些对话框来获取用户输入、显示提示信息或执行一些特定的操作。 6. 多线程支持(Multithreading Support):WinForm应用程序可以利用多线程来处理耗时操作,以保持界面的响应性。你可以使用后台线程来执行长时间运行的任务,而不会阻塞主线程,从而使用户能够继续与应用程序进行交互。 总而言之,C# WinForm应用程序是一种使用C#语言和WinForms框架构建的Windows桌面应用程序。它提供了一种简单而直观的方式来创建用户界面,并通过事件处理和数据绑定来实现交互和数据操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值