如何让应用程序仅运行一个实例(c#)
2011-02-18 16:17
543 查看
主要的思想非常简单,就是检查当前运行的进程里是否有现在要启动的应用程序的进程名称,如果有,查找两个进程的exe文件路径是否相同,如果相同,则让已运行的进程作为当前运行进程即可!
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
[STAThread]
static void Main()
{
//Application.Run(new UIForm());
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run(new UIForm());
}
else
{
//处理发现的例程
HandleRunningInstance(instance);
}
}
public static string GetMainPath(string strPath)
{
string[] ArrStr = strPath.Split('\\');
string str = "";
for (int i = 0; i < ArrStr.Length - 1; i++)
{
str += ArrStr[i];
}
return str;
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName("AsynchUI");
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (GetMainPath(process.MainModule.FileName.Replace("/","\\")) ==
GetMainPath(Application.ExecutablePath.Replace("/", "\\")))
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(
IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool
SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
if ($ != jQuery) {
$ = jQuery.noConflict();
}
var isLogined = false;
var cb_blogId = 21206;
var cb_entryId = 506886;
var cb_blogApp = "babybear315";
var cb_blogUserGuid = "185f360b-63cf-dd11-9e4d-001cf0cd104b";
var cb_entryCreatedDate = '2006/9/17 22:15:00';
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
[STAThread]
static void Main()
{
//Application.Run(new UIForm());
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run(new UIForm());
}
else
{
//处理发现的例程
HandleRunningInstance(instance);
}
}
public static string GetMainPath(string strPath)
{
string[] ArrStr = strPath.Split('\\');
string str = "";
for (int i = 0; i < ArrStr.Length - 1; i++)
{
str += ArrStr[i];
}
return str;
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName("AsynchUI");
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (GetMainPath(process.MainModule.FileName.Replace("/","\\")) ==
GetMainPath(Application.ExecutablePath.Replace("/", "\\")))
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(
IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool
SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
if ($ != jQuery) {
$ = jQuery.noConflict();
}
var isLogined = false;
var cb_blogId = 21206;
var cb_entryId = 506886;
var cb_blogApp = "babybear315";
var cb_blogUserGuid = "185f360b-63cf-dd11-9e4d-001cf0cd104b";
var cb_entryCreatedDate = '2006/9/17 22:15:00';
相关文章推荐
- 如何让应用程序仅运行一个实例(c#)
- C#如何让应用程序只有一个实例在运行
- 如何用Qt写一个同一时间只能运行一个实例的应用程序
- 如何让应用程序只有一个实例在运行?
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- 转载:C#只允许运行应用程序的一个实例的正确实现方法
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 如何用Qt写一个同一时间只能运行一个实例的应用程序
- C# 训练场(五)如何保证应用程序只运行一次(只开一个窗口)
- 关于"如何让应用程序只有一个实例在运行"的孟宪会的解决方法的疑问
- c# 应用程序只运行一个实例
- 保证C#应用程序只有一个实例在运行
- C#让应用程序只运行一个实例的几种方法
- C#建立了一个windows程序,如何防止多个程序实例运行?
- 用C#给程序加启动画面并只允许一个应用程序实例运行[转]
- 在C# WinForm中如何使当前应用程序只允许启动一个实例
- C# 防止同一个应用程序运行多个实例
- 如何使应用程序只运行一个实例
- Delphi中如何防止运行一个应用程序的多个实例
- 如何让应用程序只有一个实例在运行?