判断程序是否已经运行,使程序只能运行一个实例:

方法1:

//这种检测进程的名的方法,并不绝对有效。因为打开第一个实例后,将运行文件改名后,还是可以运行第二个实例.

 private static bool isAlreadyRunning()
{
bool b = false;
Process[] mProcs = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (mProcs.Length > 1) {
b = true;
}
return b;
}

方法2:

//线程互斥

static void Main()
{
bool canCreateNew;
Mutex m = new Mutex(true, "Mutex名,任意字符串", out canCreateNew);
if (canCreateNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
m.ReleaseMutex();
}
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"

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jin20000/archive/2008/10/24/3136791.aspx

C# winform应用程序仅能打开一个进程运行的更多相关文章

  1. winform程序限制只能打开一个进程

      有很多方案,先来最傻瓜式的  : static class Program     {         /// <summary>         /// 应用程序的主入口点.     ...

  2. C# Winform 禁止一个进程运行多次

    禁止一个进程运行多次 using System; using System.Windows.Forms; namespace StartExe { static class Program { /// ...

  3. c# 设置winform程序为默认打开软件 在运行中获取参数

    1.右键→打开方式→选择默认程序→选择winform程序 2.修改Program.cs 判断注册的事件是否存在,如果不存在则运行实例,并把参数传入MainForm里,如果存在则把参数写到txt文件中, ...

  4. [转] linux 下查看一个进程运行路径的方法

    http://blog.csdn.net/brioxu/article/details/5104736 在linux下查看进程大家都会想到用 ps -ef|grep XXX ps -aux | hea ...

  5. C#/WPF 仅启动一个进程实例

    如何实现仅启动一个 WPF 进程实例,并在打开第二个时,自动唤起之前打开的进程. 1 代码入口 在 App.xaml.cs 文件中,重写 OnStartup 方法,并添加 Mutex 进程锁. /// ...

  6. WinForm一次只打开一个程序

    WinForm如果我们希望一次只打开一个程序,那么我们在程序每次运行的时候都需要检测线程是否存在该程序,如果存在就呼出之前的窗体,C#代码如下: using System; using System. ...

  7. visual studio 设计第一个WinForm小程序

    WinForm小程序之消息框 首先打开visual studio 软件,然后[文件]-[新建]-[项目]-[Visual C#]-[Windows],选择Windows窗体应用程序,根据自己的需要修改 ...

  8. 空闲时间研究一个小功能:winform桌面程序如何实现动态更换桌面图标

    今天休息在家,由于天气热再加上疫情原因,就在家里呆着,空闲时想着,在很早以前(约3年前),产品人员跟我提了一个需求,那就是winform桌面程序的图标能否根据节日动态更换,这种需求在移动APP上还是比 ...

  9. 程序4-5 打开一个文件,然后unlink

    //http://blog.chinaunix.net/uid-24549279-id-71355.html /* ========================================== ...

随机推荐

  1. 理解JS回调函数

    我们经常会用到客户端与Web项目结合开发的需求,那么这样就会涉及到在客户端执行前台动态脚本函数,也就是函数回调,本文举例来说明回调函数的过程. 首先创建了一个Web项目,很简单的一个页面,只有一个bu ...

  2. redis参数与持久化原理

    [root@JR hx]# redis-cli info # Server redis_version:2.8.19 redis_git_sha1:00000000 redis_git_dirty:0 ...

  3. spinner下拉框组件

    方法一代码如下: <string-array name="city_name"> <item>浙江</item> <item>上海& ...

  4. 使用WordPress模板搭建博客系统

    综述: 前端展示:外观--->主题. 功能模块:插件. 遇到的问题: 1:无法加载编辑器文件: 切换下不同的wordPress模板,可能缓存文件有问题. 2:注册功能:密码重设链接无效bug-- ...

  5. hdu2825Wireless Password(ac+dp)

    链接 状压dp+ac dp[i+1][next[j]][st|tt]表示第i+1长度结点为next[j]状态为st|tt的时候的ans; dp[i+1][next[j]][st|tt]+=dp[i][ ...

  6. Android 检查手机网络是否可用

    添加网络状态权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 代 ...

  7. PHP 小方法之 过滤参数

    if (! function_exists ( 'parameter_filter' )) { function parameter_filter($str, $type = 'string', $f ...

  8. IE下div使用margin:0px auto不居中的原因

    IE下div使用margin:0px auto不居中的原因 一般在将div居中显示时,使用css: divX {margin:0 auto;} 此css在firefox下是好的,但是在ie下不起作用, ...

  9. swig之于c++

    [namespace] namespace nsTest1 { int nsAdd(int a, int b) { return a + b; } } namespace nsTest2 { int ...

  10. Copy List with Random Pointer [LeetCode]

    A linked list is given such that each node contains an additional random pointer which could point t ...