WPF防止重复运行实例

1、方法一

在app.xaml.cs下添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/// <summary> 
   /// App.xaml 的交互逻辑 
   /// </summary> 
   public partial class App : Application 
   
       System.Threading.Mutex mutex; 
  
       public App() 
       
           this.Startup += new StartupEventHandler(App_Startup); 
       
  
       void App_Startup(object sender, StartupEventArgs e) 
       
           bool ret; 
           mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret); 
  
           if (!ret) 
           
               MessageBox.Show("已有一个程序实例运行"); 
               Environment.Exit(0); 
           
  
       
   

  二、方法二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1.通过查找process的方法来控制应用程序启动。
PS:这个方法有bug:在多用户登录后,只有一个用户可以正常启动程序,也就是说,进程是跨用户的。
[java] view plain copy
int processCount = Process.GetProcessesByName("windowWPF").Where(o => o.Id != Process.GetCurrentProcess().Id).Count(); 
if (processCount > 1) 
    Environment.Exit(0); 
2. 注意mutex不能被回收,否则就无法发挥作用了。
[java] view plain copy
bool ret;   
using (System.Threading.Mutex  mutex = new System.Threading.Mutex(true, "WpfMuerterrrterterttex", out ret))   
{   
    if (!ret)   
        Environment.Exit(0);   
}   

  

 

  1.  /// <summary>  
  2.     /// App.xaml 的交互逻辑  
  3.     /// </summary>  
  4.     public partial class App : Application  
  5.     {  
  6.         System.Threading.Mutex mutex;  
  7.   
  8.         public App()  
  9.         {  
  10.             this.Startup += new StartupEventHandler(App_Startup);  
  11.         }  
  12.   
  13.         void App_Startup(object sender, StartupEventArgs e)  
  14.         {  
  15.             bool ret;  
  16.             mutex = new System.Threading.Mutex(true"ElectronicNeedleTherapySystem", out ret);  
  17.   
  18.             if (!ret)  
  19.             {  
  20.                 MessageBox.Show("已有一个程序实例运行");  
  21.                 Environment.Exit(0);  
  22.             }  
  23.   
  24.         }  
  25.     }  

 

三、方法三 已有程序窗体致前

 

        [DllImport("user32", CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindow(string cls, string win);
        [DllImport("user32")]
        static extern IntPtr SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32")]
        static extern bool IsIconic(IntPtr hWnd);
        [DllImport("user32")]
        static extern bool OpenIcon(IntPtr hWnd);

 

复制代码
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            mutex = new System.Threading.Mutex(true, "wpfdemo", out bool ret);
            if (!ret)
            {
                ActivateOtherWindow();
                //MessageBox.Show("程序已运行!");
                Environment.Exit(0);
            }
     }
复制代码
复制代码
        private static void ActivateOtherWindow()
        {
            //里面的文本改成自己程序窗口的标题
            //var other = FindWindow(null, "主页");
           var process= Process.GetProcessesByName("wpfexe").Where(o => o.Id != Process.GetCurrentProcess().Id).FirstOrDefault();
           var other= process.MainWindowHandle;
            if (other != IntPtr.Zero)
            {
                SetForegroundWindow(other);
                if (IsIconic(other))
                    OpenIcon(other);
            }
        }
复制代码

 

posted @   卖雨伞的小男孩  阅读(2295)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示