天地物流MC3090项目中使用,具体可参考该项目
--------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CEVA
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
if (!ProcessUtil.HasRunningInstance(null, "登陆CEVA_2010-11-16"))
{
FrmLogin login = new FrmLogin();
if (System.Windows.Forms.DialogResult.OK == login.ShowDialog())
{
FrmMain main = new FrmMain();
main.ShowDialog();
}
}
else
{
MessageBox.Show("程序已启动!");
}
}
}
}
-------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace CEVA
{
class ProcessUtil
{
[System.Runtime.InteropServices.DllImport("coredll")]
private static extern IntPtr FindWindow(string strclassName, string strWindowName);
/// <summary>
/// 判断该程序是否已有实例运行
/// </summary>
/// <param name="fileName">实例文件名</param>
/// <returns>是否有运行实例</returns>
public static bool HasRunningInstance(string strclassName, string strWindowName)
{
IntPtr hWnd = FindWindow(null, strWindowName);
if (hWnd != IntPtr.Zero)
{
//Console.WriteLine("程序已启动!");
return true;
}
return false;
}
}
}