C#唯一进程的处理Winform/WPF

在C#客户端(Winform/WPF)开发过程中,有的情况需要确保程序的唯一性,如果启动同时启动同一个程序多次,可能导致数据,通信等异常。下面有两种方法来实现唯一进程

1.使用进程(Process)判断

需要引入SwitchToThisWindow,用于将已启动程序切换到最前显示。

使用Process获取同名的程序,判断是否有同名但是不同Id的进程,如有,则关闭当前程序;如果没有,则进行正常启动。

具体代码示例如下:

winform

internal static class Program
{
    [DllImport("user32.dll")]
    public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
        Process current = Process.GetCurrentProcess();
        Process[] processes = Process.GetProcessesByName(current.ProcessName);

        foreach (Process process in processes)
        {
            if (process.Id != current.Id)
            { //根据进程ID排除当前进程
                MessageBox.Show("系统已启动");
                IntPtr handle = process.MainWindowHandle;
                //将原进程显示在最前
                SwitchToThisWindow(handle, true);
                current.Close();
                return;
            }
        }
        //正常启动程序
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

WPF

在App.xaml.cs中重写OnStartup程序,然后添加下面代码

  [DllImport("user32.dll")]
  public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

  protected override void OnStartup(StartupEventArgs e)
  {
      Process currentProcess = Process.GetCurrentProcess(); //获取当前进程
      //获取当前运行程序完全限定名
      string currentFileName = currentProcess.MainModule.FileName;
      Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
      foreach (Process process in processes)
      {
          if (process.Id != currentProcess.Id)
          { //根据进程ID排除当前进程
              MessageBox.Show("系统已启动");
              IntPtr handle = process.MainWindowHandle;
              //将原进程显示在最前
              SwitchToThisWindow(handle, true);
              Application.Current.Shutdown();
              return;
          }
      }
      base.OnStartup(e);
  }

执行效果:

第二次启动,显示弹窗,然后关闭之后显示第一个程序窗体。

在这里插入图片描述

2.使用互斥体(Metux)实现

基本实现方式一样,只是一开始判断使用使用互斥体Metux进行判断,选择一个字符串进行创建,如果未存在,则为True,继续创建,如果已经存在了,则为False,关闭当前进程,显示原进程。

winform

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    internal static class Program
    {
        /// <summary>
        /// 焦点切换指定的窗口,并将其带到前台
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="fAltTab"></param>
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createdNew;
            Mutex mutex = new Mutex(true, "ED4D323242424324242ERS", out createdNew); // 使用唯一的GUID作为互斥锁名称

            //
            if (!createdNew)
            {
                var current = Process.GetCurrentProcess();
                var processes = Process.GetProcessesByName(current.ProcessName);

                var process = processes.FirstOrDefault(x => x.Id == current.Id);
                if (process != null)
                {
                    MessageBox.Show("系统已启动");
                    IntPtr handle = process.MainWindowHandle;
                    SwitchToThisWindow(handle, true);
                }
                current.Close();
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

WPF

在App.xaml.cs中重写OnStartup程序,然后添加下面代码

 using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        protected override void OnStartup(StartupEventArgs e)
        {
            bool createdNew;
            Mutex mutex = new Mutex(true, "ED4D324324242ERS", out createdNew); // 使用唯一的GUID作为互斥锁名称

            if (!createdNew)
            {
                var current = Process.GetCurrentProcess();
                var processes = Process.GetProcessesByName(current.ProcessName);

                var process = processes.FirstOrDefault(x => x.Id == current.Id);
                if (process != null)
                {
                    IntPtr handle = process.MainWindowHandle;
                    SwitchToThisWindow(handle, true);
                }
                Application.Current.Shutdown();
            }

            //正常启动
            base.OnStartup(e);
        }
    }
}

实现效果:

效果一样
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海盗Sharp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值