关于程序运行一次的三种方法
2005-08-04 20:35
435 查看
经常在网上看到有人问“如何让自己程序只运行一个实例"的问题
我把以前给网友的回答总结一下:
1。
#region Mutex对象
Mutex mt=new Mutex(true,"MutexInstance");
if(mt.WaitOne(0,false))
Application.Run(new runonce());
else
MessageBox.Show("您的程序已经在运行了,不能运行两个实例!");
#endregion
2。
#region Process方法
Int32 _isProcessRunning;
_isProcessRunning = System.Diagnostics.Process.GetProcessesByName(
System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
if(_isProcessRunning != 1)
{
MessageBox.Show("您的程序已经在运行了,不能运行两个实例!");
}
else
Application.Run(new runonce());
#endregion
3。
[DllImport("kernel32")]
private static extern int GetLastError();
[DllImport("kernel32")]
private static extern int ReleaseMutex(IntPtr hMutex);
[DllImport("kernel32")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);
[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
const int ERROR_ALREADY_EXISTS = 0183;
//-------------------------------------------------------------------------
#region Api_Call CreateMutex
IntPtr hMutex;
hMutex=CreateMutex(null,false,"MutexInstance");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new runonce());
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
ReleaseMutex(hMutex);
}
#endregion
我把以前给网友的回答总结一下:
1。
#region Mutex对象
Mutex mt=new Mutex(true,"MutexInstance");
if(mt.WaitOne(0,false))
Application.Run(new runonce());
else
MessageBox.Show("您的程序已经在运行了,不能运行两个实例!");
#endregion
2。
#region Process方法
Int32 _isProcessRunning;
_isProcessRunning = System.Diagnostics.Process.GetProcessesByName(
System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
if(_isProcessRunning != 1)
{
MessageBox.Show("您的程序已经在运行了,不能运行两个实例!");
}
else
Application.Run(new runonce());
#endregion
3。
[DllImport("kernel32")]
private static extern int GetLastError();
[DllImport("kernel32")]
private static extern int ReleaseMutex(IntPtr hMutex);
[DllImport("kernel32")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);
[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
const int ERROR_ALREADY_EXISTS = 0183;
//-------------------------------------------------------------------------
#region Api_Call CreateMutex
IntPtr hMutex;
hMutex=CreateMutex(null,false,"MutexInstance");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new runonce());
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
ReleaseMutex(hMutex);
}
#endregion
相关文章推荐
- 关于php程序session_start运行很慢的解决方法
- 关于本程序无法运行的解决方法
- 【转】delphi程序只允许运行一个实例的三种方法:
- 关于在开发板上运行程序显示“-bin/sh:/XXX not found”的解决方法
- [转]Matlab中计算程序运行时间的三种常用方法
- 三种计算Python的代码块或程序的运行时间的方法比较
- C#计算程序运行时间的三种方法
- 关于eclipse运行java程序内存溢出的解决方法
- 编程小程序:1.编写程序,每次程序运行,产生5组双色球号码。每组号码升序排列,至少写出三种排序方法。
- 第12周-阅读、修改和运行关于交通工具类的程序,回答问题,以理解相关技术方法
- 关于Cocos2d-x程序运行时候提示关闭程序的框框的解决方法
- Matlab中计算程序运行时间的三种方法
- 关于开发中如何防止死等程序导致其它任务运行不了方法
- Android 应用第一次运行时,引导页面的设置方法(只让程序Acitivity运行一次的设置方法)
- 基于VSCode环境的三种使用Python运行其他程序方法
- delphi中,让程序只运行一次的方法[1]
- Android 应用第一次运行时,引导页面的设置方法(只让程序Acitivity运行一次的设置方法)
- 关于从服务器端动态写入脚本程序到客户端的三种方法
- Android 应用第一次运行时,引导页面的设置方法(只让程序Acitivity运行一次的设置方法)
- 关于 通过jlink使用jtag(或swd)下载程序成功后,keil4 uversion停止运行 的解决方法