vb.net ------------------------------------------------------------- Dim Createnew As Boolean = False Dim mtx As New System.Threading.Mutex(True, Application.ProductName, Createnew) If Not Createnew Then msgbox ("该程序已运行") '程序已经运行 End '退出 End If If Ubound(Diagnostics.Process.GetProcessesByName (Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then msgbox ("该程序已运行") '程序已经运行 End '退出 End If --------------------------------------------- c# -------------------------------------------------------- bool Createnew = false; System.Threading.Mutex mtx = new System.Threading.Mutex(true, Application.ProductName, Createnew); if (!(Createnew)) { msgbox("该程序已运行"); System.Environment.Exit(0); } if (Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0) { msgbox("该程序已运行"); System.Environment.Exit(0); }
prolist = System.Diagnostics.Process.GetProcessesByName(“程序名”);
if (prolist.Length > 1)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
if(!mt.WaitOne(0,false))
{
Console.WriteLine("程序已运行!");
Console.ReadLine();
return;
}
{
string pID = "Main"; Mutex m = new Mutex(false, pID); if(m.WaitOne(0, false) == true)
{
Application.Run(new Form1());
m.ReleaseMutex();
}
else
{
MessageBox.Show("程序已运行!");
} m.Close();
}再次运行时显示dialog。最好是象outlook Express那样的方式!
-------------------------------------------------------------
Dim Createnew As Boolean = False
Dim mtx As New System.Threading.Mutex(True, Application.ProductName, Createnew)
If Not Createnew Then
msgbox ("该程序已运行") '程序已经运行
End '退出
End If
If Ubound(Diagnostics.Process.GetProcessesByName (Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
msgbox ("该程序已运行") '程序已经运行
End '退出
End If
---------------------------------------------
c#
--------------------------------------------------------
bool Createnew = false;
System.Threading.Mutex mtx = new System.Threading.Mutex(true, Application.ProductName, Createnew);
if (!(Createnew)) {
msgbox("该程序已运行");
System.Environment.Exit(0);
}
if (Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0) {
msgbox("该程序已运行");
System.Environment.Exit(0);
}
public static void Main(string[] args)
{
//声明互斥体
System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce");
//判断互斥体是否使用中
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new FormMain());
else
MessageBox.Show("应用程序已经启动!");
} 方法2://添加引用
using System.Runtime.InteropServices;//申明
[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetLastError(); [System.Runtime.InteropServices.DllImport("kernel32")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName); [System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int ReleaseMutex(IntPtr hMutex);
const int ERROR_ALREADY_EXISTS = 0183;//调用
static void Main()
{
IntPtr hMutex;
hMutex=CreateMutex(null,false,"test");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
ReleaseMutex(hMutex);
}
}
Mutex mutex = new Mutex(true, Application.UserAppDataPath.Replace(@"\", "_"), out bOnlyOneInstance) ;
if ( !bOnlyOneInstance )
{
MessageBox.Show("系统已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) ;
return ;
}
#endregion