//参考
using System;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace HandleRunningProcess
{
/// <summary>
/// 判断是否已经有实例在运行的类
/// </summary>
public class CHandleRunningPro
{
/// <summary>
/// const int, ShowWindowAsync 使用时的参数,让窗体正常显示确保没有隐藏和最小化。
/// </summary>
private const int WS_SHOWNORMAL = 1;
/// <summary>
private bool HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置为foreground window
return SetForegroundWindow(instance.MainWindowHandle);
}
/// <summary>
/// 功能:查找相同名字的进程,有则前台显示,无则返回 false
/// 实现:1 查找相同名字的进程。
/// 2 有相同名字的进程,将其前台显示,返回 true
/// 3 无相同名字,返回 false
/// </summary>
/// <returns>有相同名字 true, 无相同名字 false</returns>
public bool HandleRunningInstance()
{
Process p = GetRunningInstance();
if (p != null)
{
HandleRunningInstance(p);
return true;
}
return false;
}
}
}