110,539
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 互斥实例
/// </summary>
private Mutex m_mt = null;
/// <summary>
/// 标识是否一个实例已经运行
/// </summary>
private bool m_bIsRun = false;
/// <summary>
///
/// </summary>
/// <param name="lpClassName"></param>
/// <param name="lpWindowName"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
///
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern IntPtr SetForegroundWindow(IntPtr hwnd);
/// <summary>
/// 构造函数
/// </summary>
public frm_Login()
{
try
{
this.MutexRun();
}
catch (Exception me)
{
DevExpress.XtraEditors.XtraMessageBox.Show(me.Message);
}
InitializeComponent();
}
#endregion
#region -初始化提取信息
/// <summary>
/// 单实例设置
/// </summary>
private void MutexRun()
{
bool bRun;
m_mt = new Mutex(true, "TestRun", out bRun);
if (!bRun)
{
//检查当前程序是否运行,如果已经运行则设置为前端显示提醒用户
IntPtr Hander = CommonGenerator.FindWindow(null, "你的程序窗口标题");
if (Hander != IntPtr.Zero)
{
CommonGenerator.SetForegroundWindow(Hander);
}
m_mt.Close();
Environment.Exit(1);
return;
}
}