实现同时只允许运行一个程序实例
2008-01-18 10:41
651 查看
方法一:
/// <summary>
/// 从这里开始运行
/// </summary>
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
//没有实例在运行
WeatherApp appInstance = new WeatherApp();
appInstance.StartMainGui();
}
else
{
//已经有一个实例在运行
HandleRunningInstance(instance);
}
}
#region 确保只有一个实例
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
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;
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
#endregion
方法二:
[STAThread]
static void Main(string[] args)
{
bool isFirst;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, "WindowAppTest", out isFirst);
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突
if (!isFirst)
{
MessageBox.Show("Exist");
Environment.Exit(1);//实例已经存在,退出程序
}
else
{
Application.Run(new Form1());
}
}
/// <summary>
/// 从这里开始运行
/// </summary>
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
//没有实例在运行
WeatherApp appInstance = new WeatherApp();
appInstance.StartMainGui();
}
else
{
//已经有一个实例在运行
HandleRunningInstance(instance);
}
}
#region 确保只有一个实例
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
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;
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
#endregion
方法二:
[STAThread]
static void Main(string[] args)
{
bool isFirst;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, "WindowAppTest", out isFirst);
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突
if (!isFirst)
{
MessageBox.Show("Exist");
Environment.Exit(1);//实例已经存在,退出程序
}
else
{
Application.Run(new Form1());
}
}
相关文章推荐
- 实现同时只允许运行一个程序实例
- 实现同时只允许运行一个程序实例
- [转]实现同时只允许运行一个程序实例
- 仅允许程序运行一个实例代码实现
- 利用互斥体(MUTEX)实现程序只允许运行一个实例
- c# Winform 如何实现程序只能同时运行一个实例
- bat脚本实例实现只允许运行一个实例(安装程序、创建快捷方式脚本)
- 使用C#实现只允许运行一个程序实例的几种方法
- QT实现只允许运行一个程序实例
- 实现只允许程序运行一个实例
- 使用进程互斥实现WinForm程序只运行一个实例(VB.Net)
- 只允许允许一个程序实例,即使是通过虚拟桌面方式连接过来的,也是只允许一个人运行。
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- C#程序只允许运行一个实例的解决方案
- 只允许一个程序运行一个实例 代码
- 利用互斥对象实现一个程序只运行一个实例的方法.
- vc只允许程序有一个实例运行
- 解决C# WINFORM程序只允许运行一个实例的几种方法详解
- [VC]在VC++中实现让程序只运行一个实例的方法且实现该实例
- 用C#给程序加启动画面并只允许一个应用程序实例运行