WINCE中只允许程序运行一个实例
2011-02-25 15:43
363 查看
.NET CF:
[DllImport("coredll.Dll")] private static extern int GetLastError(); [DllImport("coredll.Dll")] private static extern int ReleaseMutex(IntPtr hMutex); [DllImport("coredll.Dll")] 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;
static void Main() { #region Api_Call CreateMutex; IntPtr hMutex; hMutex=CreateMutex(null,false,"程序名"); if (GetLastError()!=ERROR_ALREADY_EXISTS) { Application.Run(new Frmmenu()); } else { MessageBox.Show("本程序只允许同时运行一个"); ReleaseMutex(hMutex); } #endregion }
EVC:
HANDLE hMutex=::CreateMutex(NULL,TRUE,L"LiCR"); if(hMutex!=NULL) { if(GetLastError()==ERROR_ALREADY_EXISTS) {
AfxMessageBox( "已经有一个程序运行." ); ReleaseMutex(hMutex); return FALSE; } }
大部分网站就只提供到这一步,但是在实际操作过程中,有时还是会打开多个实例,下面提供一个方式,再次单击时,打开第一个实例,经实际测试,通过
[DllImport("coredll.dll", EntryPoint = "CreateMutex", SetLastError = true)] public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitiaOwner, string lpName); [DllImport("coredll.dll", SetLastError = true)] public static extern int ReleaseMutex(IntPtr hMutex); [DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("coredll.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); public const int ERROR_ALREADY_EXISTS = 183; public const int SW_SHOW = 5; public static LoginForm MainWindow; /// <summary> /// 应用程序的主入口点。 /// </summary> [MTAThread] static void Main() { IntPtr hMutex = CreateMutex(IntPtr.Zero, false, @"GprsPatrolNtV2"); if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS) { IntPtr mainFrm = FindWindow(null, GlobalSetting.ApplicationTitle); ShowWindow(mainFrm, SW_SHOW); SetForegroundWindow(mainFrm); Application.Exit(); } else { MainWindow = new LoginForm(); Application.Run(MainWindow); ReleaseMutex(hMutex); } }
相关文章推荐
- WINCE中只允许程序运行一个实例
- WINCE中只允许程序运行一个实例
- WINCE中只允许程序运行一个实例
- WINCE中只允许程序运行一个实例
- bat脚本实例实现只允许运行一个实例(安装程序、创建快捷方式脚本)
- C#程序只允许运行一个实例的解决方案 分类: .NET 2012-12-15 12:03 438人阅读 评论(0) 收藏
- 解决C#程序只允许运行一个实例的几种方法详解
- C# 创建互斥程序_只允许运行一个程序实例
- 解决C# WINFORM程序只允许运行一个实例的几种方法详解
- vc只允许程序有一个实例运行
- C#程序只允许运行一个实例的解决方案
- [转]实现同时只允许运行一个程序实例
- C#程序只允许运行一个实例的解决方案
- c++ builder 只允许程序运行一个实例
- C# WinForm 判断程序是否已经在运行,且只允许运行一个实例,附源码
- C# Winform程序只允许运行一个实例
- C#程序只允许运行一个实例的解决方案
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- WinForm判断程序是否已经在运行,且只允许运行一个实例
- C#程序只允许运行一个实例的解决方案