1、利用Process.GetProcessesByName获得所有相同名字的Process,然后判断数组的 Length属性是否大于1,大于1则代表有一个以上的进程。如:Module Main Sub Main() Dim [me] As Process = Process.GetCurrentProcess() Dim processes() As Process = Process.GetProcessesByName([me].ProcessName) If processes.Length > 1 Then Return End If Application.EnableVisualStyles() Application.Run(New Form1) End Sub End Module2、利用Mutex创建一个跨进程的lock,如果在指定时间内不能获得lock的话就证明lock 在另一进程中,所以退出。可以根据情况指定一个较短的等待相应时间。如:Module Main Sub Main() Dim mutex As New Mutex(False, "ProcessLock") Dim locked As Boolean = Not mutex.WaitOne(100, True) If locked Then Return End If Application.EnableVisualStyles() Application.Run(New Form1) mutex.ReleaseMutex() End Sub End Module
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; } } }
{
bool bRun=true;
Mutex m=new Mutex(true,Application.ProductName,out bRun);
if (bRun)
{
Application.Run(new WMainFrame());
m.ReleaseMutex();
}
else
{
MessageBox.Show("已经有一个此程序的实例在运行","注意");
}
}
使用互斥体Mutex类型//using System.Threading;
[STAThread]
public static void Main(string[] args)
{
Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new Form1());
else
MessageBox.Show("已经过一次");
}
Length属性是否大于1,大于1则代表有一个以上的进程。如:Module Main
Sub Main()
Dim [me] As Process = Process.GetCurrentProcess()
Dim processes() As Process =
Process.GetProcessesByName([me].ProcessName)
If processes.Length > 1 Then
Return
End If Application.EnableVisualStyles()
Application.Run(New Form1)
End Sub
End Module2、利用Mutex创建一个跨进程的lock,如果在指定时间内不能获得lock的话就证明lock
在另一进程中,所以退出。可以根据情况指定一个较短的等待相应时间。如:Module Main
Sub Main()
Dim mutex As New Mutex(False, "ProcessLock")
Dim locked As Boolean = Not mutex.WaitOne(100, True)
If locked Then
Return
End If Application.EnableVisualStyles()
Application.Run(New Form1) mutex.ReleaseMutex()
End Sub
End Module
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;
}
}
}