C# 使用Mutex只有一个进程
2021/12/7 7:19:07
本文主要是介绍C# 使用Mutex只有一个进程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
引用网址:https://blog.csdn.net/weixin_30336061/article/details/96668627 ,网址2:https://blog.csdn.net/weixin_30834019/article/details/98433151
使用Mutex只有一个进程
代码
static void Main() { bool createdNew; using(Mutex mutex=new Mutex(true,Application.ProductName,out createdNew)) { if (createdNew) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else { MessageBox.Show("Only One Instance is allowed"); System.Environment.Exit(1); } } }
C# 实现程序只启动一次(多次运行激活第一个实例,使其获得焦点,并在最前端显示)...
防止程序运行多个实例的方法有多种,如:通过使用互斥量和进程名等.而我想要实现的是:在程序运行多个实例时激活的是第一个实例,使其获得焦点,并在前端显示.
主要用到两个API 函数:
- ShowWindowAsync 该函数设置由不同线程产生的窗口的显示状态。
- SetForegroundWindow 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
代码如下:
引用以下命名空间:
using System.Runtime.InteropServices; using System.Diagnostics; using System.Reflection; //***************************************************** static class Program { /// <summary> /// 该函数设置由不同线程产生的窗口的显示状态。 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分。</param> /// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。</returns> [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); /// <summary> /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。 /// </summary> /// <param name="hWnd">将被激活并被调入前台的窗口句柄。</param> /// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。</returns> [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1; /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Process instance = RunningInstance(); if (instance == null) { Form1 frm = new Form1(); Application.Run(new Form1()); } else { HandleRunningInstance(instance); } } /// <summary> /// 获取正在运行的实例,没有运行的实例返回null; /// </summary> public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { return process; } } } return null; } /// <summary> /// 显示已运行的程序。 /// </summary> public static void HandleRunningInstance(Process instance) { ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //显示,可以注释掉 SetForegroundWindow(instance.MainWindowHandle); //放到前端 } }
实现让程序只能打开一个实例(其他方法)
//=====创建互斥体法:===== bool blnIsRunning; Mutex mutexApp = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out blnIsRunning); if (!blnIsRunning) { MessageBox.Show("程序已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } //保证同时只有一个客户端在运行 System.Threading.Mutex mutexMyapplication = new System.Threading.Mutex(false, "OnePorcess.exe"); if (!mutexMyapplication.WaitOne(100, false)) { MessageBox.Show("程序" + Application.ProductName + "已经运行!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //=====判断进程法:(修改程序名字后依然能执行)===== Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { if (process.MainModule.FileName == current.MainModule.FileName) { MessageBox.Show("程序已经运行!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } }
这篇关于C# 使用Mutex只有一个进程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用
- 2024-01-23.NET开源的简单、快速、强大的前后端分离后台权限管理系统