C#/WPF 只允许一个实例程序运行并将已运行程序置顶

使用用互斥量(System.Threading.Mutex):

同步基元,它只向一个线程授予对共享资源的独占访问权。在程序启动时候,请求一个互斥体,如果能获取对指定互斥的访问权,就职运行一个实例。

实例代码:

cs 复制代码
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        [DllImport("user32.dll ")]
        //设置窗体置顶
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("User32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private static Mutex _m = null;
        private static string currentProcess = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
        /// <summary>
        /// 只允许运行一个实例
        /// </summary>
        /// <param name="mutexName"></param>
        /// <returns>true:已经存在实例 false:不存在实例</returns>
        public static bool CheckRunOneInstance(string mutexName = null)
        {

            bool result = false;
            bool mutexWasCreated = false;
            try
            {
                bool requestInitialOwnership = true;
                if (mutexName == null)
                    mutexName = currentProcess;
                _m = new Mutex(requestInitialOwnership, mutexName, out mutexWasCreated);
                if (!mutexWasCreated)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 有程序运行,设置焦点
        /// </summary>
        private void Focus()
        {
            foreach (Process process in Process.GetProcesses())
            {
                try
                {
                    if (process.ProcessName.ToLower() != currentProcess.ToLower())
                        continue;
                    IntPtr hwnd = process.MainWindowHandle;
                    if (hwnd != IntPtr.Zero)
                    {
                        SetForegroundWindow(hwnd);
                        ShowWindow(hwnd, 2);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if(CheckRunOneInstance())
            {
                Focus();
                System.Windows.Application.Current.Shutdown();
                return;
            }
            else
            {
                new MainWindow().Show();
            }
        }
    }

实例链接:

https://download.csdn.net/download/lvxingzhe3/88668552

相关推荐
weixin_520649877 小时前
WinForm数据展示组件ListView
c#
九转成圣8 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
SmartRadio8 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
laowangpython8 小时前
Rust 入门:GitHub 热门内存安全编程语言
开发语言·其他·rust·github
我叫汪枫8 小时前
在后台管理系统中,如何递归和选择保留的思路来过滤菜单
开发语言·javascript·node.js·ecmascript
_.Switch8 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
软件技术NINI8 小时前
webkit简介及工作流程
开发语言·前端·javascript·udp·ecmascript·webkit·yarn
Brendan_0018 小时前
JavaScript的Stomp.over
开发语言·javascript·ecmascript
念2348 小时前
f5 shape分析
开发语言·javascript·ecmascript
苍穹之跃8 小时前
某量JS逆向
开发语言·javascript·ecmascript