C# 只允许运行一个实例

互斥进程(程序), 简单点说,就是在系统中只能有该程序的一个实例运行. 现在很多软件都有这功能,如Maxthon 可以设置为"只允 许打开一个窗体",还有Bitcomet等. 我也是看到这些软件的这个功能才来研究这个问题的.  要实现程序的互斥,通常有三中方式,下面 用 C#  语言来实现:

实现方式一: 使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.C#实现如下:

    把program.cs文件里的Main()函数改为如下代码:

        static void Main()
        {
            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "xinbiao_a_test", out runone);
            if (runone)
            {
              run.ReleaseMutex();
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已经运行了一个实例了。");
            }
        }

  说明:程序中通过语 句 System.Threading.Mutex run = new System.Threading.Mutex(true, "xinbiao_a_test", out runone); 来申明一个互斥体变量run,其中"xinbiao_a_test"为互斥体名,布尔变量runone用来保存是否已经运行了该程序事例.

 

实现方式二:   采用判断进程的方式,我们在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同程,如是没有运行该程序,如果有就就不运行.在C#中应用System.Diagnostics名字空间中的Process类来实现,主要代码如下:

        1,在program.cs文件中添加函数如下:

        public static System.Diagnostics.Process RunningInstance() 
        {
            System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processes) //查找相同名称的进程
            {
                if (process.Id != current.Id)  //忽略当前进程
                { //确认相同进程的程序运行位置是否一样. 
                    if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @"/") == current.MainModule.FileName) 
                    { //Return the other process instance.                       
                        return process;
                    } 
                } 
            } //No other instance was found, return null. 
            return null; 
        }  

        2,把Main ()函数改为如下代码:

        static void Main()
        {
            if(RunningInstance()==null)
            { 
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已经运行了一个实例了。");
            }
        }

 

实现方式三:全局原子法,创建程序前,先检查全局原子表中看是否存在特定原子A(创建时添加的),存在时停止创建,说明该程序已运行了一个实例;不存在则运行程序并想全局原子表中添加特定原子A;退出程序时要记得释放特定的原子A哦,不然要到关机才会释放。C#实现如下:

1、申明WinAPI函数接口:

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalAddAtom(String lpString);  //添加原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalFindAtom(String lpString);  //查找原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]

        public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);  //删除原子

2、修改Main()函数如下:

        static void Main()
        {
            if (GlobalFindAtom("xinbiao_test") == 77856768) //没找到原子"xinbiao_test"
            {
                GlobalAddAtom("xinbiao_test");  //添加原子"xinbiao_test"
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("已经运行了一个实例了。");
            }                        
        }


3、在FormClosed事件中添加如下代码:

       GlobalDeleteAtom(GlobalFindAtom("xinbiao_test"));//删除原子"xinbiao_test"

 

以上为创建互斥程序(进程)的基本通用的思想,个人认为,第一种方法最好。以上所有代码都在VS.NET2005 中测试通过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#反射是一种强大的编程技术,它允许我们在运行时检查程序集、模块和类型,并动态地创建实例、调用方法、访问字段和属性等。下面是一个示例代码,展示了如何使用C#反射来加载和使用DLL。 ```csharp using System; using System.Reflection; namespace ReflectionExample { public interface IPlugin { void Run(); } public class PluginA : IPlugin { public void Run() { Console.WriteLine("PluginA is running."); } } public class PluginB : IPlugin { public void Run() { Console.WriteLine("PluginB is running."); } } class Program { static void Main(string[] args) { // 动态加载DLL并获取类型 string dllPath = "path/to/your/plugin.dll"; Assembly assembly = Assembly.LoadFile(dllPath); Type pluginType = assembly.GetType("Namespace.PluginA"); // 创建实例并调用方法 IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType); plugin.Run(); } } } ``` 上述代码中,我们定义了一个接口`IPlugin`,并在DLL中实现了两个类`PluginA`和`PluginB`来实现该接口。在`Main`方法中,我们使用`Assembly.LoadFile`方法动态加载了DLL,并使用`assembly.GetType`方法获取了`PluginA`的类型。然后,我们使用`Activator.CreateInstance`方法创建了`PluginA`的实例,并通过调用其`Run`方法来运行插件的功能。 需要注意的是,上述示例仅仅展示了C#反射的基本用法,实际应用中可能会更加复杂。在实际使用中,你需要替换`"path/to/your/plugin.dll"`为你实际的DLL路径,以及`"Namespace.PluginA"`为你实际的类型名称。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值