当前位置: 移动技术网 > IT编程>开发语言>c# > 解决C#程序只允许运行一个实例的几种方法详解

解决C#程序只允许运行一个实例的几种方法详解

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文和大家讲一下如何使用c#来创建系统中只能有该程序的一个实例运行。要实现程序的互斥,通常有下面几种方式,下面用 c# 语言来实现:方法一:使用线程互斥变量. 通过定义互斥
本文和大家讲一下如何使用c#来创建系统中只能有该程序的一个实例运行。
要实现程序的互斥,通常有下面几种方式,下面用 c# 语言来实现:
方法一:
使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.
把program.cs文件里的main()函数改为如下代码:
复制代码 代码如下:

using system;
using system.windows.forms;
using system.runtime.interopservices;
namespace nettools
{
    static class program
    {
        [dllimport("user32.dll")]
        private static extern bool flashwindow(intptr hwnd, bool binvert);
        [dllimport("user32.dll")]
        private static extern bool flashwindowex(int pfwi);
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [stathread]
        static void main()
        {
            bool runone;
            system.threading.mutex run = new system.threading.mutex(true, "single_test", out runone);
            if (runone)
            {
                run.releasemutex();
                application.enablevisualstyles();
                application.setcompatibletextrenderingdefault(false);
                frmremote frm = new frmremote();
                int hdc = frm.handle.toint32(); // write to ...
                application.run(frm);
                intptr a = new intptr(hdc);
            }
            else
            {
                messagebox.show("已经运行了一个实例了。");
                //intptr hdc = new intptr(1312810); // read from...
                //bool flash = flashwindow(hdc, true);
            }
        }
    }
}

说明:程序中通过语句 system.threading.mutex run = new system.threading.mutex(true, "single_test", out runone);来创建一个互斥体变量run,其中"single_test"为互斥体名,在此方法返回时,如果创建了局部互斥体或指定的命名系统互斥体,则布尔值runone为true;如果指定的命名系统互斥体已存在,则为 false。已命名的互斥体是系统范围的。
方法二:采用判断进程的方式,我们在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同程,如是没有运行该程序,如果有就就不运行.在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("jiaao_test") == 77856768) //没找到原子"jiaao_test"
{
globaladdatom("jiaao_test"); //添加原子"jiaao_test"
application.enablevisualstyles();
application.setcompatibletextrenderingdefault(false);
application.run(new form1());
}
else
{
messagebox.show("已经运行了一个实例了。");
}
}

3.在formclosed事件中添加如下代码:
globaldeleteatom(globalfindatom("jiaao_test"));//删除原子"jiaao_test"
--------------------------------------*-------*--------*-----------------------------------------------
以上为创建互斥程序的基本通用的思想,个人认为,第一种方法最简单。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网