C# winform应用程序仅能打开一个进程运行
判断程序是否已经运行,使程序只能运行一个实例:
方法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应用程序仅能打开一个进程运行的更多相关文章
- winform程序限制只能打开一个进程
有很多方案,先来最傻瓜式的 : static class Program { /// <summary> /// 应用程序的主入口点. ...
- C# Winform 禁止一个进程运行多次
禁止一个进程运行多次 using System; using System.Windows.Forms; namespace StartExe { static class Program { /// ...
- c# 设置winform程序为默认打开软件 在运行中获取参数
1.右键→打开方式→选择默认程序→选择winform程序 2.修改Program.cs 判断注册的事件是否存在,如果不存在则运行实例,并把参数传入MainForm里,如果存在则把参数写到txt文件中, ...
- [转] linux 下查看一个进程运行路径的方法
http://blog.csdn.net/brioxu/article/details/5104736 在linux下查看进程大家都会想到用 ps -ef|grep XXX ps -aux | hea ...
- C#/WPF 仅启动一个进程实例
如何实现仅启动一个 WPF 进程实例,并在打开第二个时,自动唤起之前打开的进程. 1 代码入口 在 App.xaml.cs 文件中,重写 OnStartup 方法,并添加 Mutex 进程锁. /// ...
- WinForm一次只打开一个程序
WinForm如果我们希望一次只打开一个程序,那么我们在程序每次运行的时候都需要检测线程是否存在该程序,如果存在就呼出之前的窗体,C#代码如下: using System; using System. ...
- visual studio 设计第一个WinForm小程序
WinForm小程序之消息框 首先打开visual studio 软件,然后[文件]-[新建]-[项目]-[Visual C#]-[Windows],选择Windows窗体应用程序,根据自己的需要修改 ...
- 空闲时间研究一个小功能:winform桌面程序如何实现动态更换桌面图标
今天休息在家,由于天气热再加上疫情原因,就在家里呆着,空闲时想着,在很早以前(约3年前),产品人员跟我提了一个需求,那就是winform桌面程序的图标能否根据节日动态更换,这种需求在移动APP上还是比 ...
- 程序4-5 打开一个文件,然后unlink
//http://blog.chinaunix.net/uid-24549279-id-71355.html /* ========================================== ...
随机推荐
- lipo 合并target为Simulator和Device编译的静态库
进入项目对应的Build目录后,以下指令: $lipo -create Debug-iphoneos/libSalamaDeveloper.a Debug-iphonesimulator/libSal ...
- Android中的动态加载机制
在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...
- MVC 中aspx的增删改查
先看总体结构 LInQ #pragma warning disable 1591 //--------------------------------------------------------- ...
- 16-underscore库(上)
第16课 underscore库 一.介绍 Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象.他弥补了 ...
- python_way ,day25 wmi
pip install wmi 如果不能安装,就使用 安装 python3 -m pip install wmi 再安装pywin32这个包 使用: import platform import w ...
- sql语句中left join、inner join中的on与where的区别
table a(id, type): id type ---------------------------------- 1 1 2 1 3 2 table b ...
- shiro的Helloworld
package shiro; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePassword ...
- Eclipse 最全快捷键
英文的
- iOS开发 贝塞尔曲线UIBezierPath
最近项目中需要用到用贝塞尔曲线去绘制路径 ,然后往路径里面填充图片,找到这篇文章挺好,记录下来 自己学习! 转至 http://blog.csdn.net/guo_hongjun1611/articl ...
- springmvc使用spring自带日期类型验证
控制器 @Controller public class MyController { // 处理器方法 @RequestMapping(value = "/first.do") ...