.net中判断该应用程序是否已经启动,防止重复启动
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace DH.Study
{
public class Start
{
[STAThread]
static void Main()
{
// 启动登录窗体和主窗体,m_bLoginSucess=true,表示登录成功
DH.Study.frmLogin frmLogin;
if (IsExistProce() == false)
{
frmLogin = new frmLogin();
frmLogin.ShowDialog();
}
else
{
return;
}
if (frmLogin.m_bLoginSucess == true)
{
DH.Study.frmMain frmMain = new frmMain();
Application.Run(frmMain);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
}
}
private static bool IsExistProce()
{
bool temp = false;
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName.ToString().Trim() == "DH.vshost")
{
MessageBox.Show("此系统已经有一个实例在运行", "系统提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
temp = true;
break;
}
}
return temp;
}
}
}
一直要抱错,因为 DH.Study.frmLogin frmLogin;这句话执行的时候系统里面就已经具有了DH.vshost进程,所以IsExistProce()
始终为True,所以始终启动不了程序,请问怎么改进谢谢?