// Refer: [C#]public Mutex(bool initiallyOwned, string name,
// out bool createdNew)
static void Main()
{
/* Assure single instance: */
bool isFirst;
System.Threading.Mutex mtx = new System.Threading.Mutex
(false, "mango.2004-01-14", out isFirst);
if (isFirst)
{
Application.Run(new Form1());
}
}
// out bool createdNew)
static void Main()
{
/* Assure single instance: */
bool isFirst;
System.Threading.Mutex mtx = new System.Threading.Mutex
(false, "mango.2004-01-14", out isFirst);
if (isFirst)
{
Application.Run(new Form1());
}
}
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection; public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")] private static extern bool
SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
}
RvHandle= FindWindow(MYAPPNAME, NIL);
if RvHandle > 0 {........
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
Process[] _processes = Process.GetProcessesByName("WlServer");
if ( _processes.Length > 1 )
{
Application.Exit();
}
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
using System.Runtime.InteropServices;
public abstract class OneInstance
{
/// <summary>
/// 用来判断一个指定的程序是否正在运行
/// </summary>
/// <param name="appId">程序名称,长一点比较好,防止有重复</param>
/// <returns>如果程序是第一次运行返回True,否则返回False</returns>
public static bool IsFirst(string appId)
{
bool ret=false;
if(OpenMutex(0x1F0001,0,appId)==IntPtr.Zero)
{
CreateMutex(IntPtr.Zero,0,appId);
ret=true;
}
return ret;
} [DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
private static extern IntPtr OpenMutex(
uint dwDesiredAccess, // access
int bInheritHandle, // inheritance option
string lpName // object name
); [DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
private static extern IntPtr CreateMutex(
IntPtr lpMutexAttributes, // SD
int bInitialOwner, // initial owner
string lpName // object name
);
}
[/code] 然后在主程序的入口方法里加上以下代码
[code]
[STAThread]
static void Main()
{
if(!OneInstance.IsFirst("My Application Test"))
{
MessageBox.Show("My application is running");
return;
}
Application.Run(new Form1());
}
可以使用单根设计模式.
保证启动类只能有一个实例.
singleton