避免重复打开程序
来源:互联网 发布:淘宝怎么设置不同价格 编辑:程序博客网 时间:2024/06/05 11:11
方法来源于网路
方法1:
public static void Main(string[] args)
{
//声明互斥体
System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce");
//判断互斥体是否使用中
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new FormMain());
else
MessageBox.Show("程序已经启动!");
}
方法2:
//添加引用
using System.Runtime.InteropServices;
//申明
[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetLastError(); [System.Runtime.InteropServices.DllImport("kernel32")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName); [System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int ReleaseMutex(IntPtr hMutex);
const int ERROR_ALREADY_EXISTS = 0183;
//调用
static void Main()
{
IntPtr hMutex;
hMutex=CreateMutex(null,false,"test");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
ReleaseMutex(hMutex);
}
}
- 避免重复打开程序
- 避免重复打开程序
- 用互斥量避免程序重复运行
- 避免VisualBasic程序重复执行
- 简简单单避免程序重复启动
- Delphi避免程序重复运行
- 防止程序重复打开
- QT避免应用程序被重复打开
- C# winform 避免窗体重复打开
- VC++ 避免重复启动程序的方法
- c# 怎么避免程序重复启动
- 如何禁止打开重复程序
- 防止C++程序重复打开
- Winform避免相同MDI子窗口重复打开的方法
- C# WINFORM 避免打开重复窗体
- MFC 判断子窗体是不是已经打开,避免重复创建
- 避免重复
- 检查程序是否已经启动,避免重复启动同一程序
- 与新疆人聊天最好别犯的错误
- 离开地球表妹
- PHP this,self 和 parent 关键字 详解
- 嵌入式Linux文件系统详细介绍
- 漫长的等待,今天又看到了点希望。唉……
- 避免重复打开程序
- PHP 数组
- 装中文pdf的 一些过程(包括一些小问题)
- 跳槽的技巧——平稳淡出公司10要点
- ASP 过滤 HTML 标签
- tcl/tk and python
- 中国手机游戏业的若干矛盾
- 5大技巧,让工作变简单
- 删除MS SQL Server 2000日志