[C#]只能运行程序的一个实例
2015-05-24 21:05
274 查看
C#实现只能运行程序的一个实例(添加在窗体的Load事件中)
1. 根据进行名限制只能运行程序的一个实例
1. 根据进行名限制只能运行程序的一个实例
System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess(); System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcessesByName(process.ProcessName); if ( processList.Length != 1 ) { System.Windows.Forms.MessageBox.Show("This computer already have one program running.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); return; }2. 根据进行名限制只能运行一个实例的另外一种写法
string modelName = Process.GetCurrentProcess().MainModule.ModuleName; string processName = System.IO.Path.GetFileNameWithoutExtension(modelName); Process[] processes = Process.GetProcessesByName(processName);//根据进程名创建进程资源数组 if ( processes.Length > 1 ) { MessageBox.Show("该程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }3. 使用互斥量Mutex实现只能运行程序的一个实例
bool exit; System.Threading.Mutex newMutex = new System.Threading.Mutex(true, "仅一次", out exit); if ( exit ) { newMutex.ReleaseMutex();//释放互斥量,可以运行新窗体 } else { MessageBox.Show("本程序已经有一个实例在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }
相关文章推荐
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# Winform 如何实现程序只能同时运行一个实例
- C# 让程序只能运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# 创建互斥进程(程序只能运行一个实例)
- C#winform判断程序已经运,且只能运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C# WINFORM判断程序是否运行,且只能运行一个实例
- c# 程序只能运行一个实例
- 【转】C#只能运行一个实例程序的方法
- C# WINFORM判断程序是否运行,且只能运行一个实例
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
- C#只能运行一个实例程序的方法
- C# WINFORM判断程序是否运行,且只能运行一个实例(转)