日期:2014-05-18 浏览次数:21021 次
bool bOnlyOneInstance = false; System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.UserAppDataPath.Replace(@"\", "_"), out bOnlyOneInstance); if (!bOnlyOneInstance) { MessageBox.Show("系统已经运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); System.Environment.Exit(0); return; }
------解决方案--------------------
判断的方法
/// <summary> /// 判断系统是否已打开 /// </summary> /// <returns></returns> private bool IsExistCurrentProcess() { bool functionReturnValue = false; bool bTemp = false; try { string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(assemblyName); foreach (Process pro in processes) { if (current.Id != pro.Id) { bTemp = true; break; // TODO: might not be correct. Was : Exit For } } functionReturnValue = bTemp; } catch { functionReturnValue = bTemp; } return functionReturnValue; }
------解决方案--------------------
用放射查找 你知道它的名字吧
------解决方案--------------------
是否运行可以遍历进行表,也可以用windows的api: FindWindow(null,"你程序的窗体名称") ;
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
如果程序窗口存在,它返回窗口的句柄。
显示窗口的windows api showWindow(Findwindow找到的窗口句柄,5);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
第二个参数nCmdShow:
#define SW_HIDE 0
#define SW_SHOWNORMAL 1
#define SW_NORMAL 1
#define SW_SHOWMINIMIZED 2
#define SW_SHOWMAXIMIZED 3
#define SW_MAXIMIZE 3
#define SW_SHOWNOACTIVATE 4
#define SW_SHOW 5
#define SW_MINIMIZE 6
#define SW_SHOWMINNOACTIVE 7
#define SW_SHOWNA 8
#define SW_RESTORE 9
#define SW_SHOWDEFAULT 10
#define SW_FORCEMINIMIZE 11
#define SW_MAX 11
------解决方案--------------------
用反射,通过窗体的名字来反射窗体实例
bool runone; System.Threading.Mutex run = new System.Threading.Mutex(true, "Frm", out runone); if (!runone) { MessageBox.Show("程序已经运行,不能重复打开!", "提示", MessageBoxButtons.OK); return; }
------解决方案--------------------
单例模式
打开A窗口
A a=A.GetInstance();
a.show();
A窗口中
public static ManagerList form = null;