Windows Mobile只运行一个实例

因为一个项目的需求,要实现程序只有一个实例运行。在网上搜了很久,最后在CSDN上面看到一回复。得到启示,完成该功能。

主要用的是互斥对象来实现。代码如下:
static class Program { [DllImport("coredll.Dll",SetLastError= true)] private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, string lpName); [DllImport("coredll.Dll",SetLastError = true)] private static extern int ReleaseMutex(IntPtr hMutex); [StructLayout(LayoutKind.Sequential)] public class SECURITY_ATTRIBUTES { public int nLength; public int lpSecurityDescriptor; public int bInheritHandle; } const int ERROR_ALREADY_EXISTS = 0183; /// <summary> /// 应用程序的主入口点。 /// </summary> [MTAThread] static void Main() { IntPtr hMutex = CreateMutex(null, false, "StandardWorkMan"); if (Marshal.GetLastWin32Error() != ERROR_ALREADY_EXISTS) { Application.Run(new FormWorkList()); } else { MessageBox.Show("已经启动了一个程序,请勿重复打开", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); ReleaseMutex(hMutex); Application.Exit(); } } }

你可能感兴趣的:(windows,String,Security,Class,mobile,attributes)