c# 软件运行一个实例

    技术2022-07-10  162

    Program类

    static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { //Application.Run的窗口不能作为登录界面,因为Application.Run起来的窗口是主窗口,主窗口适合长期显示,主窗口一旦关闭程序就终止了。 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); bool bCreateOne; //创建互斥量,只运行一个实例 System.Threading.Mutex run = new System.Threading.Mutex(true, Application.ProductName, out bCreateOne); if (bCreateOne) {

    Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //1.1 没有实例在运行 Frm_Login frmLogin = new Frm_Login(); frmLogin.ShowDialog();//ShowDialog有返回值,返回值是DialogResult if (frmLogin.DialogResult == DialogResult.OK) { frmLogin.Dispose(); int SH = Screen.PrimaryScreen.Bounds.Height; int SW = Screen.PrimaryScreen.Bounds.Width; if (SW >= 1900) { Application.Run(new Frm_MainBigs()); } else if (SW >= 1600) { Application.Run(new Frm_MainMiddle()); } else { Application.Run(new Frm_Main()); } } else if (frmLogin.DialogResult == DialogResult.Cancel) { frmLogin.Dispose(); return; } run.ReleaseMutex(); } else { //1.2 已经有一个实例在运行 MessageBox.Show("程序已经在运行中...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }

    登录界面,提交 private void btnSubmit_Click(object sender, EventArgs e) { try { if (!string.IsNullOrEmpty(txtAccount.Text.Trim()) || !string.IsNullOrEmpty(txtpwd.Text.Trim())) { IUserInfo model = new UserInfo(txtAccount.Text.Trim(), MD5Encrypt.MD5Encrypt64(txtpwd.Text.Trim())); if (model.Mark) { Global.UserName = txtAccount.Text.Trim(); DataTable dt = UserInfoBLL.Select(Global.UserName); Global.RoleID = Convert.ToInt32(dt.Rows[0][“RoleID”]); this.DialogResult = DialogResult.OK; this.Dispose(); this.Close(); } else { MessageBox.Show(“登录失败”, “温馨提示”, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show(“请完善登录信息”, “温馨提示”, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { CWriteFile.WriteDBLog(ex); }

    }
    Processed: 0.011, SQL: 9