zoukankan      html  css  js  c++  java
  • 【C#】 实现WinForm中只能启动一个实例

    如代码所示:

    方法一:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Reflection;
    using System.Runtime.InteropServices;
    
    namespace ProcessBarTest
    {
        static class Program
        {
            private const int SW_SHOW = 5;
    
            [DllImport("User32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
    
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                Process p = RunningInstance();
                if (p == null)
                {
                    Application.Run(new MainForm());
                }
                else
                {
                    HandleRunningInstance(p);
                }
            }
    
            /// <summary>
            /// Get current process
            /// </summary>
            /// <returns></returns>
            private static Process RunningInstance()
            {
                Process currentProcess = Process.GetCurrentProcess();
                Process[] processByName = Process.GetProcessesByName(currentProcess.ProcessName);
                return processByName.FirstOrDefault(process2 => (process2.Id != currentProcess.Id) && (Assembly.GetExecutingAssembly().Location.Replace("/", @"") == currentProcess.MainModule.FileName));
            }
    
            private static void HandleRunningInstance(Process p)
            {
                MessageBox.Show("已经有一个实例在运行,一台电脑只能运行一个实例!");
                ShowWindowAsync(p.MainWindowHandle, SW_SHOW);
            }
        }
    }

     方法二:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Threading;
    using System.Reflection;
    
    namespace LANChat_UI
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                bool bIsRunning;
                Mutex mutexApp = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out bIsRunning);
                if (!bIsRunning)
                {
                    MessageBox.Show("聊天程序正在运行,系统只能运行一个聊天实例!");
                }
                else
                {
                    MainForm mainForm = new MainForm();
                    mainForm.Text = Environment.UserName.ToString();
                    Application.Run(mainForm);
                }            
            }
        }
    }
    

      

  • 相关阅读:
    apue 第19章 伪终端
    apue 第18章 终端I/O
    linux IPC socket(2)
    linux IPC socket
    linux POSIX信号量
    【Luogu】【关卡2-16】线性动态规划(2017年10月)【还差三道题】
    【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】
    【Luogu】【关卡2-14】 树形数据结构(2017年10月)【AK】
    【Luogu】【关卡2-13】线性数据结构(2017年10月)【还差一道题】
    【Luogu】【关卡2-12】递推与递归二分(2017年10月)
  • 原文地址:https://www.cnblogs.com/Jins/p/3208455.html
Copyright © 2011-2022 走看看