.net cs程序仅启动一次的方法
来源:互联网 发布:cad办公室网络布线图 编辑:程序博客网 时间:2024/05/22 06:39
方法一.
private Boolean IsFirstInstance()
{
if (System.Diagnostics.Process.GetProcessesByName(Application.ProductName).Length
> 1)
{
return false;
}
return true;
}
方法二
private static bool HaveOtherInstance()
{
try
{
EventWaitHandle globalHandler = EventWaitHandle.OpenExisting("iReaper");
globalHandler.Set();
return true;
}
catch (WaitHandleCannotBeOpenedException)
{
EventWaitHandle global = new EventWaitHandle(true, EventResetMode.AutoReset, "iReaper");
Thread monitor = new Thread(new ThreadStart(delegate()
{
while (true)
{
global.WaitOne();
}
}));
monitor.IsBackground = true;
monitor.Start();
GC.KeepAlive(monitor);
return false;
}
}
方法三
<!--StartFragment-->public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
分析:方法一和方法三从本质上说 是一样的,是在当前运行进程中是否存在同名或地址相同进程,若没有则说明程序第一次运行。方法二不同,是在当前进程中另运行一个模块,同时设置一个线程同步变量。程序运行时,查看是否存在某一名称变量。
- .net cs程序仅启动一次的方法
- C#.NET客户端CS程序 只能启动一次
- 限制程序仅运行一次
- VS2010中仅一次配置opencv的方法
- VS2010中仅一次配置opencv的方法
- tomcat部署项目,仅启动一次
- “仅安装数据库软件”启动“oracleserviceorcl”的方法
- 用 .NET制作程序启动画面的两种方法。
- 大傻个 cs的用处(半条命系列MOD启动方法)
- VS2010的VC工程仅配置一次lib和include以后再无需配置的方法
- C#.net 后台cs 调用前台javascript 的方法
- DoTween 五:< 常用方法 —— 渐变动画的再次调用or仅可执行一次>
- 程序只运行一次的方法
- boost仅运行一次的例程
- 大恶人吉日嘎拉之走火入魔闭门造车之.NET 多种数据库兼容的实现方法,仅写一套程序在多种数据库上执行例子程序
- 大恶人吉日嘎拉之走火入魔闭门造车之.NET 多种数据库兼容的实现方法,仅写一套程序在多种数据库上执行例子程序
- 让程序只启动一次
- 记一次VB Windows程序的启动和退出处理
- 与对手合作
- 用 JSP 自己编写 CKEditor 图片上传服务器端代码
- 怎样解决数组越界的问题?
- struts一个action使用另一个action中的函数
- Fedora 10下的LAMP环境搭建
- .net cs程序仅启动一次的方法
- 带着十八岁的心去旅行
- 对数学归纳法的思考
- java数据类型传递探索
- 着手准备回家过年
- Java equals
- Endless
- SQL Server数据库设计表和字段的经验
- JVM参数配置大全